Logical operators in C programming including AND, OR, NOT and XOR explained with examples.

Logical Operator Truth Table

p q p&&q p||q !p p^q
0 0 0 0 1 0
0 1 0 1 1 1
1 0 0 1 0 1
1 1 1 1 0 0

C Program to demonstrate Working of Logical Operator.


// C program that shows how logical operators works.
#include <stdio.h>
int main()
{
int p = 5, q = 5, r = 10, res;
res = (p == q) && (r > q);
printf("(p== q) && (r > q) is %d \n", res);
res = (p == q) && (r < q);
printf("(p == q) && (r < q) is %d \n", res);
res = (p == q) || (r < q);
printf("(p == q) || (r < q) is %d \n", res);
res = (p != q) || (r < q);
printf("(p != q) || (r < q) is %d \n", res);
res = !(p != q);
printf("!(p != q) is %d \n", res);
res = !(p == q);
printf("!(p == q) is %d \n", res);
return 0;
}
Output:
(p == q) && (r > q) is 1
(p == q) && (r < q) is 0
(p == q) || (r < q) is 1
(p != q) || (r < q) is 0
!(p != q) is 1
!(p == q) is 0


Program Explanation:
Execution of the Program:

1. Declaration of variables:
o int p = 5, q = 5, r = 10, res;
o We have declared three integer variables p, q, and r, initializing them with 5, 5 and 10 respectively. The variable res would hold the outcome of logical operations.

2. First Logical Operation: AND (&&)
res = (p == q) && (r > q);
o Condition: (p == q) checks whether p is equal to q which is true since both are 5.
o (r > q) checks whether r is greater than q (this is true since 10 > 5).
o AND Operation: true&&true yields 1 (true).
o Output: (p == q) && (r > q) is 1

3. Second Logical Operation: AND (&&)
res = (p == q) && (r < q);
o Condition: (p == q) is true (p and q are both 5)
. o (r < q) checks whether r is less than q (this is false, for 10 is not less than 5).
o AND Operation: true&&false gives 0 (false).
o Output: (p == q) && (r < q) is 0

4. Third Logical Operation: OR (||)
res = (p == q) || (r < q);
o Condition: (p == q) is true and (r < q) is false.
o OR Operation: true || false equals to 1 (true), for only one of the conditions needs to be true for OR to return true.
o Output: (p == q) || (r < q) is 1

5. Fourth Logical Operation: OR (||)
res = (p != q) || (r < q);
o Condition: (p != q) checks if p is not equal to q (false, since both are 5).
o (r < q) is false too (for 10 is not less than 5).
o OR Operation: false || false equals to 0 (false).
o Output: (p != q) || (r < q) is 0.

6. Fifth Logical Operation: NOT (!)
res = !(p != q);
o Condition: (p != q) checks whether p is not equal to q (false, since both are 5).
o NOT Operation: !false equals 1 (true), since the NOT operator inverts the value.
o Output: !(p != q) is 1.

7. Sixth Logical Operation: NOT (!)
res = !(p == q);
o Condition: (p == q) checks whether p is equal to q (true, since both are 5).
o NOT Operation: !true equals to 0 (false).
o Output: !(p == q) is 0.

Final Output:
(p == q) && (r > q) is 1
(p == q) && (r < q) is 0
(p == q) || (r < q) is 1
(p != q) || (r < q) is 0
!(p != q) is 1
!(p == q) is 0

Conclusion:
This program highlights the workings of logical operators such as AND (&&), OR (||) and NOT (!) in the evaluation of conditions in C. Depending upon the validity of the conditions, the results will decide how to make a choice in the program using logical operators combining the conditions.


Previous Topic:-->> Relational Operators || Next topic:-->>Bitwise perators