 # C Programming Code Examples

## C > Beginners Lab Assignments Code Examples

### C Programming - C Logical Operators

``` C Programming - C Logical Operators An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Operator Meaning of Operator Example && Logial AND. True only if all operands are true If z = 8 and d = 2 then, expression ((z == 8) && (d > 8)) equals to 0. || Logical OR. True only if either one operand is true If z = 8 and d = 2 then, expression ((z == 8) || (d > 8)) equals to 1. ! Logical NOT. True only if the operand is 0 If z = 8 then, expression ! (z == 8) equals to 0. // C Program to demonstrate the working of logical operators #include <stdio.h> int main() { int x = 8, y = 8, z = 10, result; result = (x == y) && (z > y); printf("(x == y) && (z > y) equals to %d \n", result); result = (x == y) && (z < y); printf("(x == y) && (z < y) equals to %d \n", result); result = (x == y) || (z < y); printf("(x == y) || (z < y) equals to %d \n", result); result = (x != y) || (z < y); printf("(x != y) || (z < y) equals to %d \n", result); result = !(x != y); printf("!(x == y) equals to %d \n", result); result = !(x == y); printf("!(x == y) equals to %d \n", result); return 0; } Explanation of logical operator program (x == y) && (z > 8) evaluates to 1 because both operands (x == y) and (z > y) is 1 (true). (x == y) && (z < y) evaluates to 0 because operand (z < y) is 0 (false). (x == y) || (z < y) evaluates to 1 because (x = y) is 1 (true). (x != y) || (z < y) evaluates to 0 because both operand (x != y) and (z < y) are 0 (false). !(x != y) evaluates to 1 because operand (x != y) is 0 (false). Hence, !(x != y) is 1 (true). !(x == y) evaluates to 0 because (x == y) is 1 (true). Hence, !(x == y) is 0 (false). ```