These operators are used to perform logical operations on the given expressions.
There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).
Operator Example/Description
&& (logical AND) (x>5)&&(y<5)
It returns true when both conditions are true
|| (logical OR) (x>=10)||(y>=10)
It returns true when at- least one of the condition is true
! (logical NOT) !((x>5)&&(y<5))
It reverses the state of the operand “((x>5) && (y<5))”
If “((x>5) && (y<5))” is true, logical NOT operator makes it false
EXAMPLE PROGRAM FOR LOGICAL OPERATORS IN C:
#include <stdio.h>
int main()
{
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0)
{
printf("&& Operator : Both conditions are true\n");
}
if (o>p || p!=20)
{
printf("|| Operator : Only one condition is true\n");
}
if (!(m>n && m !=0))
{
printf("! Operator : Both conditions are true\n");
}
else
{
printf("! Operator : Both conditions are true. " \
"But, status is inverted as false\n");
}
OUTPUT:
&& Operator : Both conditions are true|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as false