# Logical Operators in C

Logical operators are used to perform logical operations of given expressions (relational expressions) or variables.

There are three logical operators available in C. Letâ€™s discuss one by one.

## || (Logical OR) operator

If one of the operands or expressions is true, it will return 1.

If all of them are false, it will return 0.

A

B

A || B

Example

0

0

0

(5 > 10) || (5 < 4)         Both expressions are false. so, logical OR output will be 0

0

1

1

(10 > 20) || (10 < 20)    First expression is false and second one is true. so, logical OR output will be 1

1

0

1

(10 < 20) || (10 > 100)   First expression is true and second one is false. so, logical OR output will be 1

1

1

1

(10 < 20) || (10 < 100)   Both expressions are true. so, logical OR output will be 1

## && (Logical AND) operator

If both left and right operands or expressions are true, it will return true. Otherwise, it will return false.

Note, non-zero value operands are considered as true.

A

B

A && B

Example

0

0

0

(5 > 10) && (5 < 4)         Both expressions are false. so, logical AND output will be 0

0

1

0

(10 > 20) && (10 < 20)    First expression is false and second one is true. so, logical AND output will be 0

1

0

0

(10 < 20) && (10 > 100)   First expression is true and second one is false. so, logical AND output will be 0

1

1

1

(10 < 20) && (10 < 100)   Both expressions are true. so, logical AND output will be 1

## ! (Logical NOT) operator

Logical NOT operator is used to inverse the current decision. Say, if current state is true, Logical NOT (!) operator will make it false.

A

!A

Example

0

1

!(100 < 10)   100 is greater than 10. So, it will return false.
!(false) ==> true

1

0

!(10 < 100)   10 is less than 100. So, it will return true.
!(true) ==> false

## Sample Program

Example

```#include<stdio.h>

int main()
{
int a = 5, b = 10 ,ret;

ret = ( (a <= b) || (a != b) );
// 5 <= 10 ==> true. 5 != 10 ==> true. So, 1 || 1 will return 1.
printf("Return value of above expression is %d\n",ret);

ret = ( ( a < b) && (a == b ) );
// 5 < 10 ==>true. 5 == 10 ==> false. So, 1 && 0 will return 0;
printf("Return value of above expression is %d\n",ret);

ret = ! ( ( a < b) && (a == b ) );
/*we have used the same expression here.
And its result was 0.
So !0 will be 1.*/
printf("Return value of above expression is %d\n",ret);

return 0;
}
```