C Basic
History of C
• C is procedure oriented programming lang
(POP).
• C is Developed by Dennis Ritchie in 1972 at
AT&T Bell lab.
• C Combine the features of two lang’s i.e B &
BCPL(Basic Combined programmming lang).
Features Of C
• It is procedure oriented programming lang.
• It is a power full lang because in C we can write any
program for any application.
• C is middle level lang because it combines the capabilities
of assembly lang (lower level lang)with the feature of high
level lang.
• C provides a set of 32 keywords.
• C is highly portable lang ,because c program written for one
one computer can be run on another computer.
• C is platform neutral lang i.e c programs do not get affect
because of changes of upgrades in hardware or OS.
Basic Structure Of C
• Documentation Section:- It consist of a set of
comments line which gives information about
name of program, author name and date &
time of program creation.
• Link Section:- It provides the instruction to the
compiler to link the functions from the system
lab.
eg.<stdio.h>
• Definition Section:- It is used to define all
symbolic constants.
eg. Const float pi = 3.147;
• Global Declaration Section:- It is used to declare global
[Link] varibles are used throughout the complete
program & they are declared out side of function.
eg. Int a=2;
• Main() Section:- This is the essential part of any c program,it
contains two parts.
[Link] part:- It is used to declared all the varibles
request in the c program.
[Link] part:- It will contain a statements which are
executed during run time.
- The declaration part & executable part end with
(;) & enclosed within braces{ }.
• Sub program Section:- It will contain the definition of user
define functions.
Example:-
# include<stdio.h>
#include<conio.h>
Main()
{
Int a,b,c;
Clrscr();
Printf(“Enter first no:-”);
Scanf(“%d”,&a);
Printf(“Enter second no:-”);
Scanf(“%d”,&b);
c=a+b;
Printf(“\n first no = %d”,a);
Printf(“\n Second no = %d”,b);
Printf(“\n Addition = %d”,c);
Getch();
}
Operators
• Operators is a special symbol that operates on
the operands.
eg. A+b
where a,b = operands
+ = operators
• An operator tells the computer to performed
certain operations on the given value i.e
[Link] on the no of operands, the
operators are classified as follows.
• Unary operators:- It req only one operands.
eg. ++,--
• Binary operators:- It req two operands.
eg.+,-,*,/
• Ternary operators:- It req three operands.
eg.?, ;,:
C lang supports following type of operators.
1. Arithmatic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment/Decrement operators
6. Conditional operators
7. Bitwise operators
Arithmatic Operators
• C lang provides a set of arithmatic operators
to perform various mathematical calculation.
• C provides following 5 arithmatic operators.
1. + (adds 2 values)
2. - (substracts 2nd value from 1st )
3. * (Multiplies 2 values)
4. / (Div 2nd from 1st )
5. % (Div 2nd from 1st & returns remainder)
Example
# include<stdio.h>
main()
{
int a,b;
printf(“Enter two numbers:”);
scanf(“%d%d”,&a,&b);
printf(“\n addition = %d”,a+b);
printf(“\n sub = %d”,a-b);
printf(“\n Multiple = %d”,a*b);
printf(“\n Mod = %d”,a%b);
printf(“\n Divide = %d”,a/b);
getch();
}
Relational Operators
• They are used to compare the numbers.
• The statement which contain relational
operator is called as relational expression.
• The value of relational expression is either
0(false) or 1(true)
• C supports following 6 relational operators.
• < :- (Is less Than)
• > :- (Is greater than)
• <= :- (Is less than or equal)
• >= :- (Is greater than or equal)
• == :- (Is equal to)
• != :- (Is not equal to)
Example:
# include<stdio.h>
main()
{
int a,b;
printf(“Enter 2 number:-”);
scanf(“%d%d”,&a,&b);
if(a>b)
printf(“%d is greater”,a);
else
printf(“%d is greater”,b);
if(a==b)
printf(“same”);
else
printf(“different”);
If(a%2==0)
Printf(“%d is divisible by 2”,a);
Else
Printf(“%d is not divisible by 2”,a);
Getch();
}
Assignment Operator(=)
• It is used to assign the value of expr or
variable present on RHS to the variable
present on LHS.
• Eg. a = 9;
a = a*2;
a = b + c;
Short hand assignment operator:
• When assignment opr is used along with
arithmetic opr it is called as short hand
assignment operator.
• Variable arithmetic = value;
name operator
where, arithmethic opr may be +,-,*,/,%.
Example:
main()
{
int a=2,b=3,c=7;
a+=2; /* a = a+2 */
a*=bc; /* a=a+(b*c) */
b-=5; /* b=b-5 */
c = a*b; /* can’t use SA opr */
}
Increment/Decrement operator:-
• (++,--) are unary operator which req only one
operand.
• (++) is used to add one to the given operand.
• (--) is used to sub one from the operand
Increment operator
• It is used to increment the value of operand by
1.
• It is classified as follows.
1. Prefix increment operators:- when(++) opr is
followed by the operand,it is called as prefix
increment opr.
Syntax:- ++ operand;
Prefix increment opr first add’s 1 to the operand
& than perform other operations.
• Eg. # include<stdio.h>
main()
{
int a=3;
printf(“%d”,++a);
}
o/p:- 4
2. Postfix Increment operator:- When the opr(++) is
proceeded by operand, it is called as postfix increment opr.
Syntax:- operand++;
Postfix increment opr first perform the task and
then increment the operands by 1.
Eg. #include<stdio.h>
main()
{
int a=3;
printf(“%d”,a++);
printf(“\n %d”,a);
}
o/p:- 3
4
Decrement operators
• It is used to decrement value of operand by 1.
• It is classified as follows.
1. Prefix decrement operators:- When (--)opr is
followed by the operands it is called as prefix
decrement opr.
Syntax:- --operand;
Prefix decrement opr first sub 1 from the
operand & than perform the task.
• Eg. #include<stdio.h>
main()
{
int a=3;
printf(“%d”,--a);
}
o/p:- 2
2. Postfix decrement operator:- When (--) opr is proceeded
by operand it is called postfix decrement opr.
Syntax:- operand--;
Postfix decrement opr first perform the task and than
decrements the operand by 1.
Eg. #include<stdio.h>
main()
{ int a=3;
printf(“%d”,a--);
printf(“\n %d”,a);
}
o/p: 3
2
Conditional operator:
• The ternary opr pair “?:” is called as conditional
operator which are used to write the
conditional expression.
• ?: is called as ternary opr , because it takes 3
operands as follows;
• Syntax: condition?exp1:exp2;
• Condition is evaluated first
1. If it is true(1) then exp 1 is evaluated
2. If it is false(0) then exp 2 is evaluated
Example:
#include<stdio.h>
Main()
{
int x, flag;
printf(“Enter value:”);
scanf(“%d”,&x);
Flag=(x>0)?1:0;
Printf(“%d”,flag);
}
Bitwise Operator:
• They are used for manipulate of data at bit level.
• These opr are used for testing the bits or shifting
them right or left.
• C lang supports following 5 bitwise opr
1. & :- Bitwise AND
2. | :- Bitwise OR
3. ^ :- Bitwise exclusive OR
4. << :- left shift
5. >> :- right shift
Logical Operator
• They are used to combine two or more
relational expression.
• When logical operators are used to combine the
condition, it is called as logical expression or
compound relational expression.
• C lang support following three logical opr.
1. &&(AND) :- all condition must be true
2. ||(OR) :- at least one condition must be true.
3. !(NOT) :- given conition is not true.
Example: to check whether given no is +ve
as well as even
#include<stdio.h>
Main()
{
int x;
printf(“Enter no=”);
scanf(“%d”,&x);
if (x>0 && x%2==0)
printf(“+ve and even”);
else
printf(“invalid”);
}
Special Operator:
• C supports some special operator as follows.
• Sizeof operator:- It is used to find out the no.
of bits allocated by any type of variable.
• Syntax:- int variable = sizeof(variable name)
name
Example:
#include<stdio.h>
Main()
{ int a;
float f;
char c;
dou d;
Printf(“\n size of int datatype = %d bytes”,sizeof(a));
Printf(“\n size of float datatype = %d bytes”,sizeof(f));
Printf(“\n size of char datatype = %d bytes”,sizeof(c));
Printf(“\n size of double datatype = %d bytes”,sizeof(d));
}
• Output:-
• Size of int datatype = 2 bytes
• Size of float datatype = 4 bytes
• Size of char datatype = 1 bytes
• Size of double datatype = 8 bytes
Basic point to remember in c program
• #include :- preprocessor directive
• Stdio.h :- standard i/o header
• Conio.h :- consol i/o header
• Control string :-
1. Int :- %d
2. Float :- %f
3. Char :- %c