CHAPTER 2
OPERATORS operators
Operators
1. Arithmatic operator
2. Unary operator
3. Relational operator
4. Logical operator
5. Bitwise operator
6. Assignment operator
7. Contional operator
1.Arithmatic
operator
+ = a+b = 5+2 = 7
- = a-b = 5-2 = 3
* = a*b = 5*2 = 10
/ = a/b = 5/2 = 2
% -
remainder a%b
= 5%2 = 1
Program:
# include<stdio.h>
# include<conio.h>
main()
{
int a, b ; //declaration
clrscr() ;
printf ("Enter a and b
value");
scanf("%d%d", &a,&b);
printf ("a+b = %d\n",a+b);
printf ("a-b = %d\n",a-b);
printf ("a*b = %d\n",a*b);
printf ("a/b = %d\n",a/b);
printf ("%d",a%b);
getch();
}
output
Enter
a&b value 5 3
a+b=8
a-b=2
a*b=15
a/b=1
2
2. Unary operator
(I)Increment (II)
Decrement
pre Increment
(++a) predecrement
(--a)
post Increment
(a++) postdecrement
( a--)
pre
increment post
Decrement
int a=5 int
a= 5
int b=++a //
6 int
b=a++ //6
Output
b =
6 b = 5
a =
6 a = 6
pre Decrement int a =5 int b =
--a output b =4
a = 4
|
post
Decrement int a = 5 int b = a--
output
b = 5 a = 4 |
program:
#
include<stdio.h>
#
include<conio.h>
main()
{
int
a = 5, b= ++a;
clrscr();
printf("
b value = %d\n a value = %d",b,a);
getch();
}
3.Relational operator
true - 1 false - 0
int a = 5 , b=10;
< |
a<b |
1 |
<= |
a<=b |
1 |
> |
a>b |
0 |
>= |
a>=b |
0 |
== |
a==b |
0 |
!= |
a!=b |
1 |
PROGRAM
#
include <stdio.h>
#
include <conio.h>
main()
{
int
a,b;
clrscr();
printf("enter
a & b value");
scanf("%d%d",&a,&b);
printf("a<b=%d\n",a<b);
printf("a<=b=%d\n",a<=b);
printf("a>b=%d\n",a>b);
printf("a>=b=%d\n",a>=b);
printf("a==b=%d\n",a==b);
printf("a!=b=%d",a!=b);
getch();
}
Output
enter a&b value 5 10
a<b=1
a<=b=1
a>b=0
a>=b=0
a==b=0
a!=b=1
4. Assignment operator
int a=5
+= x+=3
x=x+3 x=5+3=8
-= x-=2
x=x-2 x=8-2=6
*= x*=3
x=x*3 x=6*3=18
/= x/=2
x=x/2 x=18/2=9
program:
#include<stdio.h>
#include<conio.h>
main()
{
int
x;
clrscr();
printf("enter
x value");
scanf("%d",&x);
printf("x+=%d\n",x+=3);
printf("x-=%d\n",x-=2);
printf("x*=%d\n",x*=3);
printf("x/=%d\n",x/=2);
getch();
}
Output
enter
x value 5
x+=8
x-=6
x*=18
x/=9
5.Conditional operator
syntax
(condition)? true statement : false
statement ;
(m>=50)?"pass":
"fail";
program
#include<stdio.h>
#include<conio.h>
main()
{
int
m;
clrscr();
printf("enter
your mark");
scanf("%d",&m);
printf((m>=50)?"pass":"fail");
getch();
}
Output
enter
your mark
75>=50 pass
34>=50 fail
6.
Logical operator
&& AND
|| OR
! NOT
Truth
Table:
A |
B |
AND |
OR |
NOT |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
1 |
1 |
1 |
0 |
0 |
1 |
0 |
1 |
1 |
1 |
1 |
0 |
int a=5, int b=3
(a==5)&&(b==2)
(5==5)&&(3==2)
1
0
output - 0
(5==5)||(3==2)
1
0
output - 1
a=5 (1)
output - 0
program
#include<stdio.h>
#include<conio.h>
main()
{
int
a=5,b=3;
clrscr();
printf("%d\n",((a==5)&&(b==2)));
printf("%d\n",((a==5)||(b==2)));
printf("%d",(!a));
getch();
}
Output
0
1
0
7.Bitwise
operator
& - AND
| - OR
^ - EX-OR
~ - NOT
<< - LEFT
SHIFT
>> - RIGHT
SHIFT
& -
AND
8 4 2
1
2 -> 0 0 1 0
3 -> 0 0 1 1
-----------------------------
0 0 1
0 =>
2
| -
OR
8 4 2 1
2 -> 0 0 1
0
3 -> 0 0 1
1
--------------------------------
0 0 1 1 => 3
^ -
EX-OR
8
4 2 1
2 -> 0 0 1 0
3
-> 0 0 1 1
------------------------------------
0 0
0 1
=> 1
Not (1’s complement)
int a=2
int a=-2
a=2+1 =3
int a=-2+1=-1
a=-3
int
a=1
Left shift
a<<2 int a=2
8 4
2 1
2 0 0
1 0
a<<1
4 0 1 0 0
a<<2
8 1 0 0 0
Right shift
a>>2 int a=2
8 4
2 1
2 0 0
1 0
a>>1
1 0 0 0 1
a>>2
0 0 0 0 0
program:
#include<stdio.h>
#include<conio.h>
main()
{
int
a=2,b=3;
clrscr();
printf("a&b=%d\n",a&b);
printf("a|b=%d\n",a|b);
printf("a^b=%d\n",a^b);
printf("~a=%d\n",~a);
printf("a<<b=%d\n",a<<2);
printf("a>>b=%d\n",a>>2);
getch();
}
Output
a&b=2
a|b=3
a^b=1
~a=-3
a<<2=8
a>>2=0
0 comments:
Post a Comment