Professional Documents
Culture Documents
OPERATORS IN C
It supports vast no. of operators. Operator is symbol that instructs to perform some
action to the C compiler .Types:
o Arithmetic Operators
o Relational Operators
o Logical Operators
o Assignment Operators
o Unary Operators
o Conditional Operator / Ternary Operator
o Bitwise Operators
Arithmetic Operators: These are used to perform basic arithmetic operators like
addition, subtraction, multiplication etc
Operator Description
+ for Addition
- for subtraction
* for multiplication
/ for division
% modulus operator
Example: int a,b;
a=12;
b=13; c=a+b;
Relational Operators : Relational Operators used to compare two values. They return
either TRUE or FALSE
Operator Description
== Equal
!= Not equal
> Greater than
>= Greater than or Equal
< Less than
<= Less than or Equal
EX:10>12 returns FALSE
ASSIGNMENT OPERATORS:
“ = ” is used as assignment
1|Page
UNIT-II PROGRAMMING WITH C
Example: var a=12;
UNARY OPERATORS:
CONDITIONAL OPERATOR:
If Expression1 is true the Expression2 will execute otherwise Expression3 will execute.
LOGICAL OPERTORS:
Logical operators are used to join multiple conditional expressions and returns either
TRUE or FALSE
BITWISE OPERATORS:
| ---- Bitwise OR
SPECIAL OPERATORS:
2|Page
UNIT-II PROGRAMMING WITH C
sizeof( ) returns the size of variables.
b= sizeof(a);
printf(“size of a is %d”,b);
output: size of a is 4
EXPRESSIONS
Evalution of Expression:
Ex: x=6-12/3+3*2-1;
First pass
Step 1: 6-4+3*2-1
Step 2: 6-4+6-1
Pass 2
Step 1: 2+6-1
Step 2: 8-1
Step 3: 9
3|Page
UNIT-II PROGRAMMING WITH C
CONTROL STATEMENTS
Statements which are used to control the flow of execution of the statements based on
certain conditions are called control statements.
Conditional Statements:
These are basically used to a test condition. The result determines which block of
statements to be executed.
o if statement
o if… else statement
o if… else if…. If statement
o switch statement
if Statement
Syntax:
if(condition)
Ex:
#include<stdio.h>
void main( )
{
int a=10,b=5;
if(a>b)
4|Page
UNIT-II PROGRAMMING WITH C
{
printf(“a is greater”);
}
}
Output: a is greater
Syntax:
If(condition)
Else
Ex:
#include<stdio.h>
void main()
{
int a=10,b=12;
if(a>b)
{
printf(“a is greater than b”);
}
5|Page
UNIT-II PROGRAMMING WITH C
else
{
printf(“b is greater than a”);
}
}
O/P: b is greater than a
If…else if ….else
Use this if one of several blocks of code to executed.
Syntax:
If(condition1)
else if(condition2)
else
Ex:
#include<stdio.h>
void main()
{
6|Page
UNIT-II PROGRAMMING WITH C
int a=10,b=12,c=11;
if(a>b&&a>c)
{
printf(“a is greatest”);
}
else if(b>c)
{
printf(“b is greatest”);
}
else
{
printf(“c is greatest”);
}
}
O/P: b is greatest
SWITCH
Syntax:
switch(n)
case 1: statements;
break;
case 2: Statements;
break;
default: statements;
7|Page
UNIT-II PROGRAMMING WITH C
Ex:
#include<stdio.h>
void main()
{
var a=10,b=12,n;
scanf(“%d”,&n);
switch(n)
break;
break;
break;
break;
Output:
Addition is 22
8|Page
UNIT-II PROGRAMMING WITH C
LOOPING STATEMENTS
Types:
1. while loop
2. do while loop
3. for loop
4. for… in loop
While loop:
Syntax:
Initial value;
While(condition)
{
Statement 1;
Statement 2;
………….
Statement N;
Increment / decrement;
}
Ex:
#include<stdio.h>
void main()
{
int i=1;
while(i<=5)
{
printf(“%d\n”);
}
}
9|Page
UNIT-II PROGRAMMING WITH C
Output: 1 2 3 4 5
Do…while loop:
Syntax:
initial value;
do
{
Statement 1;
Statement 2;
………….
Statement N;
Increment / decrement;
}while(condition);
Ex:
#include<stdio.h>
void main()
{
int i=1;
do
{
printf(“%d\n”);
}while(i<=5);
}
Output: 1 2 3 4 5
For loop
When variables and constants of different types are combined in an expression then
they are converted to same data type. The process of converting one predefined type
into another is called type conversion.
The compiler converts all operands into the data type of the largest operand.
The sequence of rules that are applied while evaluating expressions are given below:
1. if a long int can represent all values of an unsigned int, the unsigned int is converted
to long int.
6. Else, if either operand is long int then other will be converted to long int.
7. Else, if either operand is unsigned int then others will be converted to unsigned int.
It should be noted that the final result of expression is converted to type of variable on
left side of assignment operator before assigning value to it.
The type conversion performed by the programmer by posing the data type of the
expression of specific type is known as explicit type conversion.
(data_type)expression;
where, data_type is any valid c data type, and expression may be constant, variable or
expression.
For example,
x=(int)a+b*d;
The following rules have to be followed while converting the expression from one type
to another to avoid the loss of information:
13 | P a g e