Logical Operators are useful in combining one or more conditions. C allows usage of three logical operators namely " &&, ||, ! ".
Logical Operators
Operator | Meaning |
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
Truth Table of Logical Operator
In the following tableif True(T) means value is one.
If False(F) means valuse is Zero.
X | Y | !X | !Y | X && Y | X || Y |
F | F | T | T | F | F |
F | T | T | F | F | T |
T | F | F | T | F | T |
T | T | F | F | T | T |
a=5,b=7;c=5;
Expression | Interpretation | Value |
(a>=5)&&(a<b) | True | 1 |
!(a==c) | False | 0 |
(a!=5)||(b>c) | True | 1 |
!(a==b) | True | 1 |
Codings for the above Example:
#include<stdio.h>
int main()
{
int a=5,b=7,c=5;
if((a>=5)&&(a<b)){printf("\"(a>=5)&&(a<b)\" = True\n");}
else{printf("\"(a>=5)&&(a<b)\" = False\n");}
if(!(a==c)){printf("\"!(a==b) or a is not equal to c\" = True\n");}
else{printf("\"!(a==b) or a is not equal to c\" = False\n");}
if((a!=5)||(b>c)){printf("\"(a!=5)||(b>c)\" = True\n");}
else{printf("\"(a!=5)||(b>c)\" = False\n");}
if(!(a==b)){printf("\"!(a==b) or a is not equal to b\" = True\n");}
else{printf("\"!(a==b) or a is not equal to b\" = False\n");}
return 0;
}