C Programming
C Programming
EPTS
{~-f{'~');
}
AsSE.M BLE R. .,
. 0
j)) -
&f
~.(y.,
~~te~j{,U/Yy~ ~r~-
• .f'r,-v µ, IX,
· rnwwnL
-~ k ¾~ .
_a , ~ if',~ ~1
o'~ cunoL l'.s I ~ un ~ o &cr9-'-·na.nr•L,0%--
&~ :-
00100100
laj(\a,,.1~ . A ~ ?' 11001 ,010 ·
-~ - 1010101
0010100
c e,:rrc -r co DE
r
oo II oo 00100
I
OOlD I I
10 }0 11 0}0010 10 IOll
00 ~· 00 O l l l 00 · t, 0011
l
Overview of C
A brief history
C is a programming language developed at "AT & T's Bell Laboratories" of USA in 1972.
It was written by Dennis Ritchie (fig 2).
~I.
'
Features of C
• Portability
• Modularity
• Extensible
• Speed
• Flexibility
• Rich Library
Advantages of C
1. C programming is the building block for many other high level programming
languages existing today
2. AC program written in one comp~ter can easily run on another computer without
making any change
5. Since C is a structured language, we can split any big problem into several sub
modules. Collection of these modules makes up a complete program. This modular
concept makes the testing and debugging easier
Structure of a C program
I
C Tokens
C tokens, Identifiers and Keywords are the basic elements of a C program. C tokens are the
basic buildings blocks in C. Smallest individual units in a C program are the C tokens. C tokens
are of six types. They are,
1. . Keywords
Keywords are those ·words whose meaning is already defined by Compiler. Th1:y cannot be
used as Variable Names. · There are 32 Keywords in C. C Keywords are also called
as Reserved words. There are 32 keywords in C. They are given below:
do if static while
2. Identifiers
Identifiers me tile names given to various program elements sL_ 1c~ as variables , arrays &
functions. Basically identifiers are the sequences of alphabets or d1gIts. .
Rules for forming identifier name
. _The constants refer to fixed values that the program may not change or modify during
its execution. Constants can be of any of the basic data types like an integer constant, a
floating c?nstant and a character constant. There is also a special type of constant called
enumeration constant. · ·
Eg:
5. Special Symbols
The symbols other than alphabets, digits and white spaces for example - [] () O, ; : * ... = #
are the special symbols.
6. Operators
Types of Operators
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operators
7. Bitwise ·operators
8. Sizeof() Operators
Arithmetic Operators
5 % Modulus · A%B
1. C allows only one variable on left hand side of= eg. c=a*b is legal, but a*b=c is not
legal.
2. Arithmetic operations are performed on the ASCII values of the characters and not on
characters themselves
3. Operators must be explicitly written.
4. Operation between same type of data yields same type of data, but operation between
integer and float yields a float result.
Example Program
#include <stdio.h>
int main()
{
int m=40,n=20, add,sub,mul,div,mod;
add = m+n;
sub = m-n;
mul = m*n; .
div = m/n;
mod = m%n;
printf("Addition of m, n is : %d\n", add);
printf("Subtraction of m, n is: %d\n", sub);
printf("Multiplication of m, n is : %d\n", mul);
printf("Division of m, n is : %d\n", div);
printf("Modulus of m, n is : %d\n", mod);
}
Output
Addition of m, n is : 60
Subtraction of m, n is : 20
Multiplication of m, n is : 800
Division of m, n.is: 2
Modulus of m, n is : 0
Relational Operators
Relational Operators are used to compare two or more operands. Operands may
be variables, constants or expression
5 -- is equal to m==n
#include <stdio.h>
int main() ·
{
int m=40;n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
Output
Logical Operators
Logical Operators are used to combine the results of two or more conditions. It is
also used to test more than one condition and make decision.
#include <stdio.h>
int main()
{
int a=40,b=20,c=30;
if ((a>b )&& (a >c))
{
printf(" a is greater than band c");
}
else
if(b>c)
priritf("b is greater than a and c");
else
prinf("c is greater than a and b");
}
Output
Conditional Operator
It itself checks the condition and executed the statement depending on the condition.
Syntax:
Example:
X=(a>b)?a:b
The '?:' operator acts as ternary operator. It first evaluate the condition, if it is true then
exp1 is evaluated, if condition is false then exp2 is evaluated. The drawback of
Assignment operator is tliat after the ? or : only one statement can occur. ·
Example Program
#include <stdio.h>
int main()
{
int x,a=5,b=3;
x = (a>b) ? a : b;
printf("x value is %d\n", x);
}
Output
x value is 5
. Bitwise Operators
Bitwise Operators are used for manipulation of data at bit level. It operates on integer
only.
3 I\
Bitwise XOR XAY . Will give 1 when one input
is 1 and other is O
#include <stdio.h>
main()
{ .
int Cl=l,c2;
C2=cl«2;
printf("Left shift by 2 bits c1<<2=%d~,c2);
}
Output
Special operators:
sizeof () operator:
#include~stdio.h>
int main()
{
int ivar = 100;
char cvar = 'a';
float fvar = 10.10;
printf("%d", sizeof(ivar));
printf("%d", sizeof(cvar));
printf("%d", sizeof(fvar));
return 0;
}
Output:
214
In the above example we have passed a variable to size of operator. ·It will print the value of
variable using sizeof() operator.
Example of Sizeof Data Type
#include<stdio.h>
int main()
{
printf("%d", siz,eof(int));
printf("%d", sizeof(char));
printf("%d", sizeof(float));
return 0;
}
Output:_
2 14
definitio~ has a c~ncept_ of 'd~ta types' which are used to define a variable before its use The
held in th:ro:~:~1~.ble will assign storage for the variable and define the type of data that ~ill be
• int
• float
• double
• char
Scope of a variable
A scope in any programming is a region of the program where a defined variable can have
its existence and beyond that variable cannot be accessed. There are three places where
variables can be declared in C programming language:
Local Variables
Variables that are declared inside a function or block are call~d local variables. They
can be used only by statements that are inside that function. Local variables are not known to
functions outside their own. Following is the example using local variables. Here all the
variables a, b und care local to main() function.
#include <stdio.h>
main ()
{
/* actual initialization*/
a= 10·
. J
b = 20;
c =a+ b;
printf ("value of a= %d, b = %d and c = %d\n", a, b, c);
}
Global Variables
Global variables are defined outside of a function, usually on top of the program. The global
variables will hold their value throughout the lifetime of your program and they can be
accessed inside any of the functions defined for the program.
A global variable can be accessed by any function. That is, a global variable is availabla for
use throughout your entire program after its declaration. Following is the example using global
and local variables:
#include <stdio.h>
/* global variable declaration*/
int g;
main ()
{
/* local variable declaration*/
int a, b;
/* actual initialization*/
a = 10;
b = 20;
g = a + b;
printf ("value of a= %d, b = %d and g = %d\n", a, b, g);
}