You are on page 1of 7

R.

Srinivasan
(Assistant Professor)
Phone: 9952218180
E-mail: srinivar@srmist.edu.in
Affiliation: Department of Computer Science and
Engineering
Operators

An operator is a symbol that tells the compiler to perform specific mathematical or


logical functions.
Types :
• Arithmetic Operators
• Increment and Decrement Operator
• Relational Operator
• Logical Operator
• Assignment Operator
• Bitwise Operator
• Other Operator (Comma, sizeof operator)
Operators: Relational Operator

Operator Description Example

.== Checks if the values of two operands are equal or (A == B) is not true.
not. If yes, then the condition becomes true.

!= Checks if the values of two operands are equal or (A != B) is true.


not. If the values are not equal, then the condition
becomes true.
> Checks if the value of left operand is greater than the (A > B) is not true.
value of right operand. If yes, then the condition
becomes true.
< Checks if the value of left operand is less than the (A < B) is true.
value of right operand. If yes, then the condition
becomes true.
>= Checks if the value of left operand is greater than or (A >= B) is not true.
equal to the value of right operand. If yes, then the
condition becomes true.
<= Checks if the value of left operand is less than or (A <= B) is true.
equal to the value of right operand. If yes, then the
condition becomes true.
#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); Output:
5 == 5 is 1
printf("%d > %d is %d \n", a, b, a > b); 5 == 10 is 0
printf("%d > %d is %d \n", a, c, a > c); 5 > 5 is 0
5 > 10 is 0
printf("%d < %d is %d \n", a, b, a < b); 5 < 5 is 0
printf("%d < %d is %d \n", a, c, a < c); 5 < 10 is 1
5 != 5 is 0
printf("%d != %d is %d \n", a, b, a != b); 5 != 10 is 1
printf("%d != %d is %d \n", a, c, a != c); 5 >= 5 is 1
5 >= 10 is 0
printf("%d >= %d is %d \n", a, b, a >= b); 5 <= 5 is 1
printf("%d >= %d is %d \n", a, c, a >= c); 5 <= 10 is 1

printf("%d <= %d is %d \n", a, b, a <= b);


printf("%d <= %d is %d \n", a, c, a <= c);
return 0; }
Operators: Logical Operator

Operator Description Example

&& Called Logical AND operator. If both the operands (A && B) is false.
are non-zero, then the condition becomes
true.

|| Called Logical OR Operator. If any of the two (A || B) is true.


operands is non-zero, then the condition
becomes true.

! Called Logical NOT Operator. It is used to reverse !(A && B) is true.


the logical state of its operand. If a condition is
true, then Logical NOT operator will make it false.
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result; Output:
result = (a == b) && (c > b); (a == b) && (c > b) is 1
printf("(a == b) && (c > b) is %d \n", result); (a == b) && (c < b) is 0
result = (a == b) && (c < b); (a == b) || (c < b) is 1
printf("(a == b) && (c < b) is %d \n", result); (a != b) || (c < b) is 0
result = (a == b) || (c < b); !(a != b) is 1
printf("(a == b) || (c < b) is %d \n", result); !(a == b) is 0
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a == b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}

You might also like