Introduction to
C Programming
Language
Overview
C is a general-purpose, high-level language
By Dennis M. Ritchie
For UNIX operating system at AT&T Bell Labs
first implemented on 1972
Originally used primarily for Systems
programming.
Why use C?
Easy to learn
Structured language
It produces efficient programs
It can handle low-level activities
It can be compiled on a variety of computer platforms
Lets you write programs that resembles everyday English
C
Programming
Structure
Miles to
Kilometers
Conversion
Preprocessor Directives
#include<stdio.h>
It modify the text of a C program before it is compiled.
Many actions that are necessary in a computer program are not
defined directly by C.
every C implementation contains collections of useful functions and symbols
called libraries
The #include directive gives a program access to a library.
2 most common directives are given below.
Preprocessor Directives
#define KMS_PER_MILE 1.609
Replacing each occurrence of KMS_PER_MILE into the text 1.609 from
the C program before compilation begins.
This line of code:
kms = KMS_PER_MILE * miles;
Is similar to:
kms = 1.609 * miles;
Using constant macro like KMS_PER_MILe, it is easier to understand and
maintain the program.
Comments
Provides supplementary information that makes a program easier to understand.
Comments are like helping text in your C program and they are ignored by the
compiler. They start with /* and terminate with the characters */ as shown below.
/* my first program in C */ /* - This can be applied to single line
// my first program in C // - This is best for single line
/* my first /* - This can also be applied to multiple lines
program
in C */
Function Main
Marks the beginning of the main function where program execution begins.
Every program has a main function
The remaining lines of the program form the body of the function which is enclosed in
braces {, }.
Function body has two parts: declarations and executable statements.
Int main(void)
{
// function declaration – to tell the compiler
// executable statements – to tell the processor
}
The line int indicates that main function returns an integer value (0) to the Operating System
when normal execution finishes.
Reserve Words
Contains a number of different words classified as;
reserve words,
identifiers from standard libraries, and
names for memory cells
It has special meaning in C and cannot be used for other purposes.
Reserve words:
int, void, double, return
Standard Identifiers:
printf, scanf
User-defined Identifiers:
KMS_PER_MILE, main, miles, kms
The Use of Uppercase and
Lowercase Letters
C compiler consider usage significant.
The names Rate, rate and RATE are treated by the compiler as different identifiers.
Adopting a consistency of in the use of letters is helpful to the readers of your
program.
You will see that all reserved words in C and the names of all standard library functions
use only lower case letters.
Program Style Choosing Identifier Names
Most programs are examined or studied by someone other than the original
programmers.
In industry, programmer spend more time on program maintenance (updating
and modifying the program).
A program that is neatly stated and whose meaning is clear makes every one’s
job simplier
Pick a meaningful name for a user-defined identifier
identifier salary would be a good name for a memory cell used to store a person’s
salary, whereas the identifier s or bagel would be a bad choice.
If an identifier consists of two or more words, placing the underscore character ( _
) between words will improve the readability of the name
dollars_per_ hour rather than dollarsperhour.
you are more likely to make a typing error in a longer name
often the compiler cannot help you detect your error with regard to this matter.
Tokens in C
A C program consists of various tokens and a token is either a
keyword, an identifier, a constant, a string literal, or a symbol
printf ( “ Hello, World! \n ” ) ;
identifier String Literal
symbol
Semicolons
In a C program, the semicolon is a statement terminator. That is,
each individual statement must be ended with a semicolon. It
indicates the end of one logical entity.
printf(“Hello, World! \n”);
return 0;
Identifiers
A C identifier is a name used to identify a variable, function, or any
other user-defined item.
letter A to Z, a to z
underscore '_‘ followed by zero or more letters
C does not allow punctuation characters such as @, $, and % within
identifiers
Whitespace in C
A line containing only whitespace, possibly with a comment, is
known as a blank line, and a C compiler totally ignores it.
Whitespace is the term used in C to describe blanks, tabs, newline
characters and comments.
int age;
Whitespace
Variable Definition in C
A variable definition tells the compiler where and how much
storage to create for the variable. A variable definition specifies a
data type and contains a list of one or more variables of that type
as follows.
type variable_list; // Syntax
int x = 25; // valid statement
10 = 20; // invalid statement; would generate compile-time error
Data Types
Data types in c refer to an extensive system used for declaring
variables or functions of different types. The type of a variable
determines how much space it occupies in storage and how the bit
pattern stored is interpreted.
int, 2 or 4 bytes, -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
char , 1 byte, -128 to 127 or 0 to 255
float | 4 bytes | 6 digits precision
double | 8 bytes | 15 digits precision
long double | 10 bytes | 19 digits precision
Arithmetic Operators
+ , Adds two operands.
- , Subtracts second operand from the first.
* , Multiplies both operands.
/ , Divides numerator by de-numerator.
% , Modulus Operator and remainder of after an integer
division.
++ , Increment operator increases the integer value by one.
-- , Decrement operator decreases the integer value by one.
Relational Operators
== , Checks if the values of two operands are equal or not. If yes, then the
condition becomes true.
!= , Checks if the values of two operands are equal or not. If the values are
not equal, then the condition becomes true.
> , Checks if the value of left operand is greater than the value of right
operand. If yes, then the condition becomes true.
< , Checks if the value of left operand is less than the value of right
operand. If yes, then the condition becomes true.
>= , Checks if the value of left operand is greater than or equal to the value
of right operand. If yes, then the condition becomes true.
<= , Checks if the value of left operand is less than or equal to the value of
right operand. If yes, then the condition becomes true.
Logical Operators
&& , Called Logical AND operator. If both the operands are non-
zero, then the condition becomes true.
|| , Called Logical OR Operator. If any of the two operands is non-
zero, then the condition becomes true.
! , Called Logical NOT Operator. It is used to reverse the logical
state of its operand. If a condition is true, then Logical NOT operator will
make it false.
Assignment Operators
= , Simple assignment operator. Assigns values from right side operands to left side
operand
+= , Add AND assignment operator. It adds the right operand to the left operand
and assign the result to the left operand.
-= , Subtract AND assignment operator. It subtracts the right operand from the left
operand and assigns the result to the left operand.
*= , Multiply AND assignment operator. It multiplies the right operand with the left
operand and assigns the result to the left operand.
/= , Divide AND assignment operator. It divides the left operand with the right
operand and assigns the result to the left operand.
%= , Modulus AND assignment operator. It takes modulus using two operands and
assigns the result to the left operand.