Ex. No.
3 Operators
11.09.24
Aim
To study and practice the various types Operators of C Programming language.
Operators also classified into
1. Arithmetic Operator
2. Relational Operator
3. Logical Operator
4. Ternary or Conditional operator
5. Increment and Decrement operator
6. Bitwise Operator
7. Sizeof Operator
8. Comma Operator
Programs
1. Arithmetic Operator
#include < stdio.h >
int main()
{
int a = 10,b = 5, c;
c = a+b; //Addition
printf("a + b = %d \n",c);
c = a-b; //Subtraction
printf("a - b = %d \n",c);
c = a*b; //Multiplication
printf("a * b = %d \n",c);
c = a/b; //Division
printf("a / b = %d \n",c);
c = a%b; //Modulo Division
printf("a % b = %d \n",c);
return 0;
}
Output: a + b = 15
a-b=5
a * b = 50
a/b=2
a%b=0
2. Relational Operator
#include < stdio.h >
int main()
{
int a = 5, b = 5, c = 10;
printf("%d = = %d is %d \n", a, b, a = = b);
printf("%d = = %d is %d \n", a, c, a = = c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
Output: 5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
3. Logical Operator
#include < stdio.h >
int main()
{
int a= 10, b = 20, c= 30;
printf ("(a>b) && (a<c) is %d", (a>b) && (a<c));
printf ("(a>b) || (a<c) is %d", (a>b) || (a<c));
printf ("!(a>b) is %d", !(a>b));
return 0;
}
Output: (a>b) && (a<c) is 0
(a>b) || (a<c) is 1
!(a>b) is 1
4. Ternary or Conditional operator
#include
int main()
{
int a=5,b=10;
a>b? printf("a is Greater") : printf("b is Greater");
return 0;
}
Output: b is Greater
5. Increment and Decrement operator
Output: a1=5, a=6
b1=9, b=9
c1=4, c=3
d1=1, d=1
6. Bitwise Operator
#include < stdio.h >
int main()
{
a=9 //equal to 1001
b = 12 //equal to 1100
print ("a & b = ",a & b)
print ("a | b = ",a | b)
print("a ^ b = ",a ^ b)
print("~a = ",~a)
print("a << 2 = ",a << 2)
print("a >> 2 = ",a >> 2)
return(0)
}
Output: a & b = 8
a | b = 13
a^b=5
~a = -10
a << 2 = 36
a >> 2 = 2
7. Sizeof Operator
#include < stdio.h >
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return(0)
}
Output: Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte
Conclusion
Thus the programs executed and verified successfully.