Logical Operators in C
C language provides three logical operators which include the logical AND (&&), logical OR (||) and logical NOT (!) operator. These operators are used to perform logical operations on two operands and thus are binary operators. However, logical NOT (!) has only one operand and hence is a unary operator. All these operators perform the function what is signified by their name. Operands can be constants or variables containing integer quantities, floating-point quantities or characters. In addition, the operands can be relational expressions and/or arithmetic expressions. A logical operator along with its operand(s) gives rise to Logical Expression. The output of a logical expression is a Boolean value (true=non-zero or false=0) decided by the nature of operation. Table 5.2 enumerates the working of each logical operator.
Table 1 : Usage and meaning of Logical operators
Program 1 shows these operators in action which is followed by the screenshot of the output.
#include <stdio.h>
void main()
{
int i, j, k;
i = 10;
j = 20;
k = (i < 11) && (j < 21);
printf(“(i<11) && (j<21)is %d\n”,k);
k = (i > 10) || (j > 20);
printf(“(i>10) || (j>20)is %d\n”,k);
k = !i;
printf(“!i is %d\n”,k);
}
Program 1 : Program shows logical operators in action