The following table shows all the logical operators supported by the C language. Assume variable x holds 21, y holds 10 and variable z holds 0 then.
&& and || are binary operators, whereas, ! is a unary operator.
Operator Name | Syntax | Description | Example |
---|---|---|---|
Logical AND | x && y | If both the operands are non-zero, then the condition becomes true. | z = (x && y); z = 1 |
Logical OR | x || y | If any of the two operands is non-zero, then the condition becomes true. | z = (x || y); z = 1 |
Logical NOT | !x | It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. | z = !x; z = 0 |
&& and || are binary operators, whereas, ! is a unary operator.
#include <stdio.h>
int main()
{
int x = 21;
int y = 10;
int z = 0;
printf("Logical operators demo\n");
printf("Value of x is %d\n", x );
printf("Value of y is %d\n", y );
printf("Value of z is %d\n", z );
z = (x && y);
printf("Logical AND (z = (x && y)): %d\n", z);
z = (x || y);
printf("Logical OR (z = (x || y)): %d\n", z);
z = !x;
printf("Logical NOT (z = !x)): %d\n", z);
return 0;
}
The output of the above program would be:
Logical operators demo
Value of x is 21
Value of y is 10
Value of z is 0
Logical AND (z = (x && y)): 1
Logical OR (z = (x || y)): 1
Logical NOT (z = !x)): 0
Related topics:
Overview of Operators in C | Arithmetic Operators in C | Relational Operators in C | Bitwise Operators in C | Compound Assignment Operators in C | Conditional Operators in C | Miscellaneous Operators in C | Operator Precedence and Associativity in C
List of topics: C Programming
No comments:
Post a Comment