Operators:
C is
supported to following types of operators.
- Arithmetical Operators
- Relational Operators
- Comma Operator
- Assignment Operator
- Conditional Operators
- Logical Operators
- Bitwise Operators
- Increment And Decrement Operators
- Unary Operators
- Binary Operators
1.Arithmetical
Operators:
+ ,
- , *, / ,% are called Arithmetical
Operators.
Ex: Write
a Program of sum of two Numbers
Program-1
|
Program-2
|
Program-3
|
Program-4
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;
int c;
clrscr();
a=20;
b=10;
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
c=a+b;
printf(“\nsum=%d”,c);
getch();
}
Output:
a=20
b=10
sum=30
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
a=20;
b=10;
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
c=a+b;
printf(“\nsum=%d”,c);
getch();
}
Output:
a=20
b=10
sum=30
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a=20,b=10,c;
clrscr();
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
c=a+b;
printf(“\nsum=%d”,c);
getch();
}
Output:
a=20
b=10
sum=30
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a=20,b=10;
clrscr();
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
printf(“\nsum=%d”,a+b);
getch();
}
Output:
a=20
b=10
sum=30
|
Note:
no.of lines reduced
|
Note:no.of lines reduced
|
Note:no.of lines reduced and Memory reduced
|
|
Program-5
|
Program-6
|
||
#include<stdio.h>
#include<conio.h>
void main()
{
int a=20,b=10;
clrscr();
printf(“\na=%d \nb=%d \nsum=%d”,a,b,a+b);
getch();
}
Output:
a=20
b=10
sum=30
|
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\na=%d \nb=%d \nsum=%d”,20,10,20+10);
getch();
}
Output:
a=20
b=10
sum=30
|
||
Note:
no.of lines reduced
|
Note:
no.of lines reduced
|
//Write
a Program of demonstrate Arithmetical Operators
Program-1
|
Program-2
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;
int c;
clrscr();
a=20;
b=10;
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
c=a+b;
printf(“\nsum=%d”,c);
c=a-b;
printf(“\nsub=%d”,c);
c=a*b;
printf(“\nprod=%d”,c);
c=a/b;
printf(“\ndiv=%d”,c);
c=a%b;
printf(“\nrem=%d”,c);
getch();
}
Output:
a=20
b=10
sum=30
sub=10
prod=200
div=2
rem=0
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;
int c;
clrscr();
a=20;
b=10;
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
printf(“\nsum=%d”,a+b);
printf(“\nsub=%d”, a-b);
printf(“\nprod=%d”, a*b);
printf(“\ndiv=%d”, a/b);
printf(“\nrem=%d”, a%b);
getch();
}
Output:
a=20
b=10
sum=30
sub=10
prod=200
div=2
rem=0
|
//Write a Program find area and perimeter of square.
S
| ||
S
|
S
| |
S
|
perimeter=4*s
Program-
|
#include<stdio.h>
#include<conio.h>
void main()
{
float
s=2.3,area,peri;
clrscr();
printf("\nside of the
square=%f",s);
area=s*s;
peri=4*s;
printf("\narea of the
square=%f",area);
printf("\nperi of the
square=%f",peri);
getch();
}
Output:
side of the square=2.300000
area of the square=5.290000
peri of the square=9.200000
|
Note:
Control
String
|
Decimal places
|
%0.0f
%0.1f
%0.2f
%0.3f
%0.4f
%0.5f
%0.6f or
%f
|
No decimal
places
1
2
3
4
5
6
|
Ex:
Program-
|
#include<stdio.h>
#include<conio.h>
void main()
{
float
s=2.3,area,peri;
clrscr();
printf("\nside of the
square=%0.2f",s);
area=s*s;
peri=4*s;
printf("\narea of the
square=%0.2f",area);
printf("\nperi of the
square=%0.2f",peri);
getch();
}
Output:
side of the square=2.30
area of the square=5.29
peri of the square=9.20
|
scanf(
):
It
is a predefined function and it is used to accept the data by the user at run
time.
The source
code is available in stdio.h
Syntax:
scanf(“control
string”,&var1,&var2….);
Ex: //Write
a Program of sum of two Numbers and accept the a,b values by the user at runtime.
Program-1
|
Program-2
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“enter the value of a:”);
scanf(“%d”,&a);
printf(“enter the value of b:”);
scanf(“%d”,&b);
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
printf(“\nsum=%d”,a+b);
getch();
}
Result:
Input:
Enter the value of a:25
Enter the value of b:5
Output:
a=25
b=5
sum=30
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“enter the value of a,b:”);
scanf(“%d%d”,&a,&b);
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
printf(“\nsum=%d”,a+b);
getch();
}
Result:
Input:
Enter the value of a,b:25 5
Output:
a=25
b=5
sum=30
|
//Write a Program find area and perimeter of square and accept side of the square by the user at run time.
S
| ||
S
|
S
| |
S
|
perimeter=4*s
Program-
|
#include<stdio.h>
#include<conio.h>
void main()
{
float s=2.3,area,peri;
clrscr();
printf("enter side of the square:");
scanf("%f",&s);
printf("\nside of the square=%0.2f",s);
area=s*s;
peri=4*s;
printf("\narea of the square=%0.2f",area);
printf("\nperi of the square=%0.2f",peri);
getch();
}
Result:
Input:
Enter side of the square:2.5
Output:
side of the square=2.50
area of the square=6.25
peri of the square=10.00
|
2.Comma operator:
it is used to
separate the expression or statements.
Program
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr(),
a=35,
b=1,
printf(“\na=%d”,a),
printf(“\nb=%d”,b),
c=a+b,
printf(“\nsum=%d”,c),
getch();
}
Output:
a=35
b=1
sum=36
|
3.
Relational Operators:
<
, >, <=,>=,==,!= are called Relation operators.
The
expression is true in between two variable then it can be display 1 otherwise
zero.
//Write
a Program of demonstrate Relational Operators
Program-1
|
Program-2
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a=96,b=5,c;
clrscr();
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
c=a<b;
printf(“\n a<b =%d”,c);
c=a>b;
printf(“\n a>b =%d”,c);
c=a<=b;
printf(“\n a<=b =%d”,c);
c=a>=b;
printf(“\nd a>=b =%d”,c);
c=a==b;
printf(“\nr a==b =%d”,c);
c=a!=b;
printf(“\n a!=b =%d”,c);
getch();
}
Output:
a=96
b=5
a<b=0
a>b=1
a<=b=0
a>=b=1
a==b=0
a!=b=1
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a=96,b=5,c;
clrscr();
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
printf(“\n %d<%d=%d”,a,b,a<b);
printf(“\n %d>%d=%d”,a,b,a>b);
printf(“\n %d<=%d=%d”,a,b,a<=b);
printf(“\n %d>=%d=%d”,a,b,a>=b);
printf(“\n %d==%d=%d”,a,b,a==b);
printf(“\n %d!=%d=%d”,a,b,a!=b);
getch();
}
Output:
a=96
b=5
95<5=0
95>5=1
95<=5=0
95>=5=1
95==5=0
95!=5=1
|
4.
Assignment Operator
We
can assign a value for the variable.
Ex:
a=25, x=’t’ …etc.
5. Conditional
Operators:
Syntax:
Condition?
exp1:exp2;
Ex:56>32?printf(“true”):printf(“false”);
//write a programme to demonstrate
Conditional Operators.
#include<stdio.h>
#include<conio.h>
void
main()
{
clrscr();
56>32?printf(“true”):printf(“false”);
getch();
}
Output:
True
//write a program to find greatest
numbers of two numbers using conditional operator.
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a,b,max;
clrscr();
printf(“enter
the value of a,b:”);
scanf(“d%d”,&a,&b);
max=a>b?a:b;
printf(“\max=%d”,max);
getch();
}
Result:
Input:
enter
the value of a,b:96 45
Output:
Max=96
6.
Logical operators
&& , || , ! are called Logical
operators
Logical AND(&&)
Truth
table:
Condition 1
|
Condition 2
|
Output
|
T(1)
|
T(1)
|
T(1)
|
T(1)
|
F(0)
|
F(0)
|
F(0)
|
T(1)
|
F(0)
|
F(0)
|
F(0)
|
F(0)
|
By the above table we understand condition 1 & 2 are true then output is true remaining
cases are false.
Logical OR(||)
Truth
table:
Condition 1
|
Condition 2
|
Output
|
T(1)
|
T(1)
|
T(1)
|
T(1)
|
F(0)
|
T(1)
|
F(0)
|
T(1)
|
T(1)
|
F(0)
|
F(0)
|
F(0)
|
By the above table we understand the condition 1 & 2 are false then output is false remaining cases are true.
Logical NOT(!)
Truth
table:
Condition 1
|
Output
|
T(1)
|
F(0)
|
F(0)
|
T(1)
|
If
the condition is true then output is false.
If
the condition is false then output is true.
//write
a program demonstrates Logical operators.
Program
|
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\n\n Logical AND:”);
printf(“\n1&&1 =%d”,1&&1);
printf(“\n1&&0 =%d”,1&&0);
printf(“\n0&&1 =%d”,0&&1);
printf(“\n0&&0=%d”,0&&0);
printf(“\n\n Logical OR:”);
printf(“\n1||1 =%d”,1||1);
printf(“\n1||0 =%d”,1||0);
printf(“\n0||1 =%d”,0||1);
printf(“\n0||0=%d”,0||0);
printf(“\n\n Logical NOT:”);
printf(“\n!1 =%d”,!1);
printf(“\n!0=%d”,!0);
getch();
}
Output:
Logical AND:
1&&1=1
1&&0=0
0&&1=0
0&&0=0
Logical OR:
1||1=1
1||0=1
0||1=1
0||0=0
Logical NOT:
!1=0
!0=1
|
7.Bitwise
operators:
&
,|,^,<<,>> are called bitwise operators.
Bitwise AND(&)
Truth
table:
Condition 1
|
Condition 2
|
Output
|
Ex:10&2:
10- 0000 1010
02- 0000 0010
10
& 2 - 0000 0010
=1*21+0*20
=2
|
1
|
1
|
1
|
|
1
|
0
|
0
|
|
0
|
1
|
0
|
|
0
|
0
|
0
|
By the above table we understand condition 1 & 2 are true then output is true remaining
cases are false.
Bitwise OR(|)
Truth
table:
Condition 1
|
Condition 2
|
Output
|
Ex:10|2:
10-
0000 1010
02- 0000 0010
10
| 2 - 0000 1010
=1*23+0*22+1*21+0*20
=10
|
1
|
1
|
1
|
|
1
|
0
|
1
|
|
0
|
1
|
1
|
|
0
|
0
|
0
|
By the above table we understand condition 1 & 2 are false then output is false
remaining cases are true.
Bitwise XOR(^)
Truth
table:
Condition 1
|
Condition 2
|
Output
|
Ex:10^2:
10-
0000 1010
02- 0000 0010
10
^ 2 - 0000 1000
=1*23+0*22+0*21+0*20
=8
|
1
|
1
|
0
|
|
1
|
0
|
1
|
|
0
|
1
|
1
|
|
0
|
0
|
0
|
By the above table we understand
condition1
& 2 are true then output is false
condition1
& 2 are false then output is false
and the Remaining case are true.
Bitwise leftshift(<<):
The bits can be shifted from right to left
Ex:10<<2
=1*25+0*24+1*23+0*22+0*21+0*20
=40
Bitwise Right shift(>>):
The bits can be shifted from left to Right
Ex:10>>2
=1*21+0*20
=2
//write
a program demonstrates Bitwise operators.
Program
|
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\n10&2=%d”,10&2);
printf(“\n10|2=%d”,10|2);
printf(“\n10^2=%d”,10^2);
printf(“\n10<<2=%d”,10<<2);
printf(“\n10>>2=%d”,10>>2);
getch();
}
Output:
10&2=2
10|2=10
10^2=8
10<<2=40
10>>2=2
|
8.
Increment and Decrement operators:
Increment
operators:
++
is called an increment operator and the value is increased by 1.
C is
supported following types of increment
operators
Pre
increment operator
Post
increment operator
Pre increment operator:
++
is placed before the variable is called pre increment operator.
Ex:++a,++b…etc.
//write
a program demonstrates pre increment operators.
Program
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a=15;
clrscr();
printf(“\nBefore increment a=%d”,a);
++a;
printf(“\nAfter increment a=%d”,a);
getch();
}
Output:
Before increment a=15
After increment a=16
|
Post increment operator:
++
is placed after the variable is called post increment operator.
Ex:a++,b++…etc.
//write
a program demonstrates post increment operators.
Program
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a=15;
clrscr();
printf(“\nBefore increment a=%d”,a);
a++;
printf(“\nAfter increment a=%d”,a);
getch();
}
Output:
Before increment a=15
After increment a=16
|
Difference
pre and post increment operators.
In
pre increment operators the value can be increased first after that it display, when increasing and display with in the single statement .
In
post increment operators the value can be displayed first after that it is increased, when increasing and display with in the single statement .
Program
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a=15;
clrscr();
printf(“\n a=%d”,a);
printf(“\n ++a=%d”,++a);
printf(“\n a++=%d”,a++);
printf(“\n ++a=%d”,++a);
printf(“\n a++=%d”,a++);
printf(“\n ++a=%d”,++a);
printf(“\n a=%d”,a);
getch();
}
Output:
a=15
++a=16
a++=16
++a=18
a++=18
++a=20
a=20
|
Decrement
operators:
- - is called an decrement operator and the value is decreased by 1.
C is
supported following types of decrement
operators
Pre
decrement operator
Post
decrement operator
Pre decrement operator:
- -
is placed before the variable is called pre decrement operator.
Ex:-
-a,- -b…etc.
//write
a program demonstrates pre decrement operators.
Program
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a=15;
clrscr();
printf(“\nBefore decrement a=%d”,a);
- - a;
printf(“\nAfter decrement a=%d”,a);
getch();
}
Output:
Before decrement a=15
After decrement a=14
|
Post decrement operator:
- - is placed after the variable is called post decrement operator.
Ex:a-
-,b- - …etc.
//write
a program demonstrates post decrement operators.
Program
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a=15;
clrscr();
printf(“\nBefore decrement a=%d”,a);
a - -;
printf(“\nAfter increment a=%d”,a);
getch();
}
Output:
Before increment a=15
After increment a=14
|
Difference
pre and post decrement operators.
In
pre decrement operators the value can be decreased first after that it is
display, when decreasing and display with in the single statement .
In
post decrement operators the value can be displayed first after that it is
decreased, when decreasing and display with in the single statement .
Program
|
#include<stdio.h>
#include<conio.h>
void main()
{
int a=15;
clrscr();
printf(“\n a=%d”,a);
printf(“\n - -a=%d”,- -a);
printf(“\n a- -=%d”,a- -);
printf(“\n - -a=%d”,- -a);
printf(“\n a- -=%d”,a- -);
printf(“\n - -a=%d”,- -a);
printf(“\n a=%d”,a);
getch();
}
Output:
a=15
- -a=14
a- -=14
- -a=12
a- -=12
- -a=10
a=10
|
9.
Unary operators.
The operator
is preceding with one operand is called Unary operators.
Ex:
++a ,a++, - -a, a - -,-a..etc
10. Binary
operators.
The operator
is preceding with two operands is called Binary operators.
Ex: a+b,
a*b, a&&b…etc
No comments:
Post a Comment