Logical Operators in C
In C programming, the logical programming are used to perform logical operations like a boolean values that are either true or false. The three logical operators are:
&& logical AND : Returns true if both operands are true.
|| logical OR : Returns true if at least one of the operands is true.
! logical NOT : Returns the opposite boolean values of the operand.
Examples for logical AND (&&) operator:
Program 1:
#include<stdio.h>
int main()
{
int a,b;
a=10; // true
b=20; // true
printf("The value for AND operator:%d",a&&b);
}
int main()
{
int a,b;
a=10; // true
b=20; // true
printf("The value for AND operator:%d",a&&b);
}
Output:
The value for AND operator:1
Program 2:
#include<stdio.h>
int main()
{
int a,b;
a=1; // true
b=0; // false
printf("The value for AND operator:%d",a&&b);
}
int main()
{
int a,b;
a=1; // true
b=0; // false
printf("The value for AND operator:%d",a&&b);
}
Output:
The value for AND operator:0
Examples for logical OR (||) operator:
Program:
#include<stdio.h>
int main()
{
int a,b;
a=10; // true
b=0; // false
printf("The value for OR operator:%d",a||b);
}
int main()
{
int a,b;
a=10; // true
b=0; // false
printf("The value for OR operator:%d",a||b);
}
Output:
The value for OR operator:1
Examples for logical NOT(!) operator:
Program:
#include<stdio.h>
int main()
{
int a,b;
a=1; // It gives opposite value 0
b=0; // It gives opposite value 1
printf("The value for NOT operator:%d",!a);
}
int main()
{
int a,b;
a=1; // It gives opposite value 0
b=0; // It gives opposite value 1
printf("The value for NOT operator:%d",!a);
}
Output:
The value for NOT operator:0