There are three logical operators in C language. Assume variable A and B holds 1 and 0 respectively then –
Operator | Description | Example |
---|---|---|
&& | AND operator (the condition becomes true if both the operands are non-zero) | (A && B) is false. |
|| | OR Operator (the condition becomes true if any of the two operands is non-zero) | (A || B) is true. |
! | NOT Operator (this operator reverse the logical state of operand. For example: If condition is true, then NOT operator will make it false) | !(A && B) is true. |
Example: Check both numbers are greater the 10 or not
1 2 3 4 5 6 7 8 9 10 11 | #include < stdio.h > int main() { int a, b, big; a = 20; b = 20; if (a > 10 && b > 10) // if condition is true move inside if-statement { printf("a and b both are greater then 10"); } return 0; } |
Output
1 | a and b both are greater then 10 |
Example: Check both numbers are greater the 10 or not. Also check any one number is greater then 10 or not
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include<stdio.h> int main() { int a, b, big; a = 20; b = 5; if (a >10 && b>10) // if condition is false { printf("a and b both are greater then 10"); } if (a >10 || b>10) // if condition is true move inside if-statement { printf("Either a or b is greater then 10"); } return 0; }</stdio.h> |
Output
1 | Either a or b is greater then 10 |