Logical Operators in C Programming Language
Notes:
Logical Operators in C Programming Language:
Logical operators are also known as logical connectives. They are used to connect one or more relational expressions or conditions. They accept Boolean operands i.e. true or false, on evaluation they yield the result either true (1) or false (0)
Note: 0 indicates false and any non zero value indicates true
&&: Logical And Operator:
If both LHS and RHS operands are true then the result will be true, in all other cases the result will be false
LHS RHS LHS && RHS
true true true
true false false
false true false
false false false
||: Logical Or Operator:
If both LHS and RHS operands are false then the result will be false, in all other cases the result will be true
LHS RHS LHS || RHS
true true true
true false true
false true true
false false false
!: Logical Not Operator:
If the given operand is false, then the result will be true. Vice versa
RHS ! RHS
true false
false true
Example Code:
#include <stdio.h>
int main()
{
printf("%d\n",(3<4) && (4<5)); // 1
printf("%d\n",(3<4) && (4>5)); // 0
printf("%d\n",(3>4) && (4<5)); // 0
printf("%d\n",(3>4) && (4>5)); // 0
printf("\n");
printf("%d\n",(3<4) || (4<5)); // 1
printf("%d\n",(3<4) || (4>5)); // 1
printf("%d\n",(3>4) || (4<5)); // 1
printf("%d\n",(3>4) || (4>5)); // 0
printf("\n");
printf("%d\n",!(3<4)); // 0
printf("%d\n",!(3>4)); // 1
printf("%d\n",!0); // 1
printf("%d\n",!1); // 0
printf("%d\n",!(10)); // 0
return 0;
}