Logical Operators in C | Logical AND in C | Logical Or in C | Logical NOT in C
There are three logical operators.
1. Logical AND in C (&&)
Logical AND (&&) is used to combine two relational expressions. If any input of this operator is 0, output will be 0.
Truth table for Logical AND operator
Input1 | Input2 | Output |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Example
12 >4 && 12>5 = 1
12 >4 && 12<5 = 0
12 <4 && 12<5 = 0
2. Logical OR in C (||)
Logical OR operator || is used to combine two relational expressions. If any input of this operator is 1, output will be 1.
Truth table for Logical OR operator
Input1 | Input2 | Output |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Example
13 >4 || 12>5 = 1
13 >4 || 12<5 = 1
13 <4 || 12<5 = 0
3. Logical NOT in C (!)
Logical NOT (!) is used to reverse the output of a relational or logical expression.
Truth table for Logical NOT operator
Input | Output |
0 | 1 |
1 | 0 |
Example
!(12 >4 && 12>5) = 0
!(12<5) = 1
!(12 <3 || 12<5) = 1
Program to demonstrate logical operators in C. |
#include<stdio.h>
int main()
{
int a,b,c;
|
a=10
|
b=3
|
printf(”\n%d”, (a>0 && b>0); /*1*/
|
printf(”\n%d”, (a<0 && b>0); /*1*/
|
printf(”\n%d”, (a>0 || b<0); /*1*/
|
printf(”\n%d”, (a<0 && b<0); /*1*/
|
printf(”\n%d”, !(a>0); /*1*/
|
printf(”\n%d”, !(a<0 && b>0); /*1*/
|
getch();
return (0);
}
|