m2 Pop
m2 Pop
Operand?
● Arithmetic operators
● Relational Operators
● Equality Operators
● Logical Operators
● Unary Operators
● Conditional Operators
● Bitwise Operators
● Assignment operators
● Comma Operator
● Sizeof Operator
Arithmetic operators:
This operator used for numeric calculation. These are of either Unary arithmetic
operator, Binary arithmetic operator. Where Unary arithmetic operator required only
one operand such as +,-, ++, --,!, tiled. And these operators are addition, subtraction,
multiplication, division. Binary arithmetic operator on other hand required two
Note: Modulus operator is used to find the reminder of the integer division. This cannot
be used on float and double operands.
#include <stdio.h>
void main()
float a=5.0,b=2.0,A,S,M,D;
int ID,R;
A=a+b;//A=7.0
S=a-b;//S=3.0
M=a*b;//M=10.0
D=a/b;//D=2.5
printf("\n%f\n%f\n%f\n%f",A,S,M,D);
int c,d;
c=a;
d=b;
ID=c/d;//ID=2
R=c%d;//R=1
printf("\n%d\n%d",ID,R);
}
Relational operators:
• Also known as a comparison operator, it is an operator that compares two values.
• Relational operators return true or false value, depending on whether the conditional
relationship between the two operands holds or not.
Equality operators
• C language supports two kinds of equality operators to compare their operands for
strict equality or inequality. They are equal to (==) and not equal to (!=) operator.
• The equality operators have lower precedence than the relational operators.
void main()
int a=10,b=20;
printf("\n %d == %d = %d",a,b,a==b);//0
printf("\n %d != %d = %d",a,b,a!=b);//1
Logical operators
• Operator used with one or more operand and return either value zero (for false) or
one (for true). The operand may be constant, variables or expressions. And the
expression that combines two or more expressions is termed as logical expression.
• C language supports three logical operators. They are- Logical AND (&&), Logical OR
(||) and Logical NOT (!).
• As in case of arithmetic expressions, the logical expressions are evaluated from left to
right.
Example:
#include <stdio.h>
void main()
int a, b;
void main()
{
int a=5,b=5,c=10,R;
R=(a==b)&&(c>b);
printf("(a==b)&&(c>b) is %d\n",R);//1
R=(a==b)&&(c<b);
printf("(a==b)&&(c<b) is %d\n",R);//0
R=(a==b)||(c<b);
printf("(a==b)||(c<b) is %d\n",R);//1
R=!(a!=b);
printf("!(a!=b) is %d\n",R);//1
R=!(a==b);
printf("!(a==b) is %d\n",R);//0
}
Unary operator
• C language supports three unary operators. They are unary minus(-), increment(++)
and decrement operators(--).
• The increment operator is a unary operator that increases the value of its operand by
1.
Unary minus:
• When an operand is preceded by a minus sign, the unary operator negates its value.
Example:
#include <stdio.h>
void main()
int a,b=10;
a=-(b);
printf("%d",a);//-10
Example:
y=x++
y=x;
x=x+1;
y=x;
x=x-1;
Example:
y=++x
x=x+1;
y=x;
x=x-1;
y=x;
Example:
#include <stdio.h>
void main()
int a=10;
printf("++a=%d\n",++a);//1
printf("--a=%d\n",--a);//10
printf("a++=%d\n",a++);//10
printf("a--=%d\n",a--);//11
Conditional operator:
• The conditional operator operator (?:) is just like an if .. else statement that can be
written within expressions.
Here, exp1 is evaluated first. If it is true then exp2 is evaluated and becomes
the result of the expression, otherwise exp3 is evaluated and becomes the result
of the expression.
#include <stdio.h>
void main()
int age=17;
void main()
{
int a=10,b=5,c=16,large;
large=(a>b)?a:b;
printf(“%d”,large);
#include <stdio.h>
void main()
int a=10,b=5,c=16,large;
large=(a>b)?(a>c?a:c):(b>c?b:c);
printf(“%d”,large);
Bitwise operator
• Bitwise operators perform operations at bit level. These operators include: bitwise
AND, bitwise OR, bitwise XOR and shift operators.
• The bitwise AND operator (&) is a small version of the boolean AND (&&) as it
performs operation on bits instead of bytes, chars, integers, etc. Bit in the first operand
is ANDed with corresponding bit in the second operand.
• The bitwise NOT (~), or complement, is a unary operation that performs logical
negation on each bit of the operand. By performing negation of each bit, it actually
produces the ones' complement of the given binary value.
• The bitwise XOR operator (^) performs operation on individual bits of the
operands. The result of XOR operation is shown in the table
• Bitwise left shift(<<): When we apply left shift, every bit in the operand is shifted to
left by one place.
if x= 0001 1101
• Bitwise right shift(>>): When we apply right shift, every bit in the operand is
shifted to right by one place.
if x= 0001 1101
#include<stdio.h>
void main()
{
int a=27,b=39;
printf(“a&b=%d\n”,a&b);//3
printf(“a|b=%d\n”,a|b);//63
printf(“~a=%d\n”,~a);//-28
printf(“a^b=%d\n”,a^b);//60
printf(“a<<1=%d\n”,a<<1);//54
printf(“b>>1=%d\n”,b>>1);//19
}
Assignment operator
• While the equal sign (=) is the fundamental assignment operator, C also supports
other assignment operators that provide shorthand ways to represent common variable
assignments. They are shown in the table.
#include <stdio.h>
void main()
{ int a=5,c=10;
printf("c/=a=%d\n",c/=a); //c=c/a,c=2
printf("c*=a=%d\n",c*=a); //c=c*a,c=10
printf("c-=a=%d\n",c-=a); //c=c-a,c=5
printf("c+=a=%d\n",c+=a); //c=c+a,c=10
printf("c&=a=%d\n",c&=a); //c=c&a,c=0
printf("c^=a=%d\n",c^=a); //c=c^a,c=5
printf("c<<=a=%d\n",c<<=a); //c=c<<a,c=160
printf("c>>=a=%d\n",c>>=a); //c=c>>a,c=5}
Comma operator
• The comma operator in C takes two operands.
• It works by evaluating the first and discarding its value, and then evaluates the
second and returns the value as the result of the expression.
• Among all the operators, the comma operator has the lowest precedence. For
example,
x = (++a, b+=a);
++a=> a=a+1
a=3
b+=a=> b=b+a
b=3+3=6
Size of operator
• The operator returns the size of the variable, data type or expression in bytes.
• 'sizeof' operator is used to determine the amount of memory space that the
variable/expression/data type will take.
#include <stdio.h>
void main()
int a;
float b;
double c;
char d;
printf("size=%u\n",sizeof(a));
printf("size=%u\n",sizeof(b));
printf("size=%u\n",sizeof(c));
printf("size=%u\n",sizeof(d));}
Type Conversion
• Type conversion and type casting of variables refers to changing a variable of one
data type into another.
• While type conversion is done implicitly, casting has to be done explicitly by the
programmer. We will discuss both of them here.
• Type conversion is done when the expression has variables of different data types.
So to evaluate the expression, the data type is promoted from lower to higher level
where the hierarchy of data types can be given as: double, float, long, int, short and
char.
For ex,
float x;
int y = 3;
x = y;
Now, x = 3.0,
Type Casting
• Type casting is also known as forced conversion. It is done when the value of a
higher data type has to be converted in to the value of a lower data type. For example,
we need to explicitly type cast an integer variable into a floating point variable.
float salary = 10000.00;
int sal;
if-Statement
• First the test expression is evaluated. If the test expression is true, the statement of
if block (statement 1 to n) are executed otherwise these statements will be skipped and
the execution will jump to statement x.
#include<stdio.h>
void main()
{
int age;
scanf("%d",&age);
if(age>=18)
}
}
#include<stdio.h>
void main()
char ch;
scanf("%c",&ch);
if(ch>0)
}}
if-else Statement
In the if-else construct, first the test expression is evaluated. If the expression is true,
statement block 1 is executed and statement block 2 is skipped. Otherwise, if the
expression is false, statement block 2 is executed and statement block 1 is ignored. In
any case after the statement block 1 or 2 gets executed the control will pass to
statement x. Therefore, statement x is executed in every case.
#include <stdio.h>
void main()
{
int a;
scanf("%d",&a);
if(a%2==0)
else
#include <stdio.h>
void main()
char ch = 'C';
else
#include <stdio.h>
void main()
int a=100,b=20;
if(a<b)
else
printf("a is largest of 2 numbers");
WAP to enter any character. If entered in upper case then display in lower case
and vice versa.
#include<stdio.h>
void main()
char ch;
scanf("%c",&ch);
if(ch>='A'&&ch<='Z')
else
The year number must be divisible by four – except for end-of-century years, which
must be divisible by 400.
Example: This means that the year 2000 was a leap year, although 1900 was not
#include<stdio.h>
void main()
int year;
scanf("%d",&year);
if((year%4==0)&&(year%100!=0)||(year%400==0))
printf("Leap year");
else
}
}
if-else if Statement
void main()
int a,b;
scanf("%d,%d",&a,&b);
if(a==b)
else if(a>b)
else
Example:
void main()
{
int a;
if(a=10)//The condition should e a==10. Here instead of checking the condition ,the
value is being assigned to a.
printf("Equal");
else
printf("not equal");
void main()
int a;
scanf("%d",&a);
if(a>0)
else if(a<0)
else
printf("The numer is eqaul to zero"); }
A company decides to give bonus to its employee. If the employee is male he gets
5% bonus, if female she gets 10% bonus. If salary is less than 10,000, he/she
gets extra 2% bonus on salary. WAP to calculate the bonus and display the salary
which has to be given to the employee
#include<stdio.h>
void main()
{
char ch;
float sal,bonus,amt;
scanf("%c",&ch);
scanf("%f",&sal);
if(ch=='m')
bonus=0.05*sal;
else
bonus=0.10*sal;
if(sal<10000)
bonus=bonus+0.20*sal;
amt=sal+bonus;
printf("The salary which employee will get is %f",amt);
Marks>=75=Distinction
void main()
{ int marks;
scanf("%d",&marks);
if(marks>=75)
printf("Distinction");
else if(marks>=60&&marks<75)
printf("First class");
else if(marks>=50&&marks<60)
printf("Second class");
else if(marks>=50&&marks<40)
printf("Third class");
else
printf("Fail"); }
WAP to find largest of 3 numbers.
void main(){
int a,b,c;
printf("Enter a,b,c\n");
scanf("%d,%d,%d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("a is greater");
else
printf("c is greater");
else if(b>c)
printf("b is greater");
else
printf("c is greater");
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter a,b,c\n");
scanf("%d,%d,%d",&a,&b,&c);
if(a>b&&a>c)
printf("a is greater");
if(b>a&&b>c)
printf("b is greater");
if(c>a&&c>b)
printf("c is greater");
}
WAP to enter the marks of 4 subject out of 100 and then calculate total,average
and display the grade obtained.
void main() {
int m1,m2,m3,m4,total;
float avg;
scanf("%d,%d,%d,%d",&m1,&m2,&m3,&m4);
total=m1+m2+m3+m4;
avg=total/4;
if(avg>=75)
printf("Distinction");
else if(avg>=60&&avg<75)
printf("First class");
else if(avg>=50&&avg<60)
printf("Second class");
else if(avg>=50&&avg<40)
printf("Third class");
else
printf("Fail");
}
Switch case
Step 2: The evaluated value is matched against all the present cases.
Step 3A: If the matching case value is found, the associated code is executed.
Step 3B: If the matching code is not found, then the default case is executed if
present.
Step 4A: If the break keyword is present in the case, then program control breaks
out of the switch statement.
Step 4B: If the break keyword is not present, then all the cases after the
matching case are executed.
Example:
char grade=‘C’;
switch(grade)
{ case 'A':
printf("\n Excellent");
break;
case 'B':
printf("\n Good");
break;
case 'C':
printf("\n Fair");
break;
default:
break;
char ch;
scanf("%c",&ch);
switch(ch)
case 'A':
break;
case 'E':
break;
case 'I':
break;
case 'O':
break;
case 'U':
break;
default:printf("%c is not a vowel",ch);
WAP to enter a number from 1-7 and display the corresponding day of the week
using switch case.
void main() {
int day;
scanf("%d",&day);
switch(day) {
case 1:printf("\nSunday");
break;
case 2:printf("\nMonday");
break;
case 3:printf("\nTuesday");
break;
case 4:printf("\nWednesday");
break;
case 5:printf("\nThursday");
break;
case 6:printf("\nFriday");
break;
case 7:printf("\nSaturday");
break;
#include<stdio.h>
void main()
int num,rem;
scanf("%d",&num);
rem=num%2;
switch(rem)
case 0:printf("\nEven");
break;
case 1:printf("\nOdd");
break;
}}
Iterative statements
Iterative statements are used to repeat the execution of a list of statements, depending
on the value of an integer expression. In this section, we will discuss all these
statements.
• While loop
• Do-while loop
• For loop
while loop
The while loop is used to repeat one or more statements while a particular condition is
true.
In the while loop, the condition is tested before any of the statements in the statement
block is executed.
If the condition is true, only then the statements will be executed otherwise the control
will jump to the immediate statement outside the while loop block.
#include<stdio.h>
void main()
{
int i=1,sum=0;
while(i<=10)
{
sum=sum+i;
i++;
printf("\nsum=%d",sum);
#include<stdio.h>
void main()
int i=1;
while(i<=20)
printf("*");
i++;
{
int i,j,sum=0;
scanf("%d",&i);
scanf("%d",&j);
while(i<=j)
{
sum=sum+i;
i++;
}
printf("\nsum=%d",sum);
int i=1,large,num;
while(i<=5)
scanf("%d",&num);
large=num>large?num:large;
i++;
void main()
{ int num,sum,count;
float avg;
scanf("%d",&num);
while(num!=-1)
count++;
sum+=num;
scanf("%d",&num);
avg=(float)sum/count;
printf("sum=%d\n",sum);
printf("Avg=%f",avg); }
do-while loop
The do-while loop is similar to the while loop. The only difference is that in a do-while
loop, the test condition is tested at the end of the loop.
The body of the loop gets executed at least one time (even if the condition is false).
The do while loop continues to execute whilst a condition is true. There is no choice
whether to execute the loop or not. Hence, entry in the loop is automatic there is only a
choice to continue it further or not.
The major disadvantage of using a do while loop is that it always executes at least
once, so even if the user enters some invalid data, the loop will execute.
do-while loops are widely used to print a list of options for a menu driven program.
Void main()
{ int n,i=1,sum;
float avg;
scanf("%d",&n);
do
sum+=i;
i++;
}while(i<=n);
avg=(float)sum/n;
printf("Sum=%d\n",sum);
printf("Avg=%f",avg); }
scanf("%d",&n);
i=1;
do
printf("\n|\t%d\t|\t%d\t|\t%d\t|",i,i*i,i*i*i);
i++;
}while(i<=n); }
void main()
{ int m=1900,n=1920;
do
if(((m%4==0)&&(m%100!=0))||(m%400==0))
m++;
}while(m<=n); }
WAP to read a character until * is encountered. Also count the number of upper
case,lower case and numbers entered.
void main()
{ char ch;
int lowers=0,uppers=0,numbers=0;
scanf("%c",&ch);
do
{
if(ch>='A'&&ch<='Z')
uppers++;
if(ch>='a'&&ch<='z')
lowers++;
if(ch>='0'&&ch<='9')
numbers++;
scanf("%c",&ch);
}while(ch!='*');
printf("\nCount of upper case entered is %d",uppers);
for loop
statement block;
Statement Y;
• When a for loop is used, the loop variable is initialized only once. With every
iteration of the loop, the value of the loop variable is updated and the condition is
checked. If the condition is true, the statement block of the loop is executed else, the
statements comprising the statement block of the for loop are skipped and the control
jumps to the immediate statement following the for loop body. Updating the loop
variable may include incrementing the loop variable, decrementing the loop variable or
setting it to some other value like, i +=2, where i is the loop variable.
void main()
{ int i,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d\t",i);
void main()
{ int i,n;
scanf("%d",&n);
for(i=0;i<=n;i++)
printf("%d\t",i);
void main()
{ int i,n;
scanf("%d",&n);
for(i=4;i<=n;i+=4)
printf("%d\t",i);
}}
Pass 2: 1 2 3 4 5
Pass 3: 1 2 3 4 5
Pass 4: 1 2 3 4 5
Pass 5: 1 2 3 4 5
void main()
int i,j;
for(i=1;i<=5;i++)
{
printf("\nPass %d",i);
for(j=1;j<=5;j++)
{ printf("\t%d",j); }
}}
**********
**********
**********
**********
**********
void main()
int i,j;
for(i=1;i<=5;i++)
printf("\n");
for(j=1;j<=10;j++)
{ printf("* "); }
}}
**
***
****
*****
void main()
int i,j;
for(i=1;i<=5;i++)
printf("\n");
for(j=1;j<=i;j++)
printf("*");
}}}
1
12
123
1234
12345
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{ printf("%d",j); }}}
1
22
333
4444
55555
void main()
int i,j;
for(i=1;i<=5;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{ printf("%d",i); }}}
0
12
345
6789
void main()
int i,j,count=0;
for(i=1;i<=4;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{ printf("%d",count++); }}}
AB
ABC
ABCD
ABCDE
ABCDEF
void main()
char i,j;
for(i=65;i<=70;i++)
printf("\n");
for(j=65;j<=i;j++)
{ printf("%c",j); }}}
12
123
1234
12345
void main()
int N = 5,i,j,k;
printf(" ");
printf("%d ",k+1);
printf("\n");
void main()
{ int n,i;
printf("Enter any number");
scanf("%d",&n);
for(i=0;i<=10;i++)
printf("\n%d X %d = %d",n,i,i*n); }
WAP to print all numbers from m to n, thereby classifying them as even or odd.
void main()
{ int m,n,i;
scanf("%d",&m);
scanf("%d",&n);
for(i=m;i<=n;i++)
if(i%2==0)
printf("\n%d is even",i);
else
printf("\n%d is odd",i);
}
break statement
• The break statement is used to terminate the execution of the nearest enclosing
loop in which it appears.
• When compiler encounters a break statement, the control passes to the statement
that follows the loop in which the break statement appears. Its syntax is quite simple,
just type keyword break followed with a semi-colon.
break;
• In switch statement if the break statement is missing then every case from the
matched case label to the end of the switch, including the default, is executed.
while(…) do
{ {
….. …..
if(condition) if(condition)
break; break;
….. …..
} }while(…);
for(…) for(…)
{ {
….. …..
if(condition) for(…)
break; {
….. if(condition)
} break;
Stmnt y
continue statement
• When the compiler encounters a continue statement then the rest of the
statements in the loop are skipped and the control is unconditionally transferred
to the loop-continuation portion of the nearest enclosing loop. Its syntax is quite
simple, just type keyword continue followed with a semi-colon.
continue;
• If placed within a for loop, the continue statement causes a branch to the code
that updates the loop variable. For example,
int i;
for(i=1;i<=10;i++)
{ if(i==5)
continue;
printf("\t %d",i); }
while(…) do
{ {
….. …..
if(condition) if(condition)
continue; continue;
….. …..
} }while(…);
for(…) for(…)
{ {
….. …..
if(condition) for(…)
continue; {
….. if(condition)
} continue;
goto statement
• Here label is an identifier that specifies the place where the branch is to be
made. Label can be any valid variable name that is followed by a colon (:).
• Note that label can be placed anywhere in the program either before or after the
goto statement. Whenever the goto statement is encountered the control is
immediately transferred to the statements following the label.
• If the label is placed after the goto statement then it is called a forward jump
and in case it is located before the goto statement, it is said to be a backward
jump.
• Goto statements make the program code complicated and render the program
unreadable.
Example:
scanf("%d", &num);
if (num != 999)
if(num < 0)
sum += num;
*****End*****