C programming language has logical operators for logical expressions. The output of a logical operation is a Boolean value – true or false.

In a logical expression, you have one or more operands and each has a truth value, depending on the truth value of each operand the logical operator decides the final truth value of the logical expression.

Contents

## Logical Operators

A list of logical operators in C language is given below. The minimum numbers of operands required by each of the operators are also given in the same table.

- AND (&&)
- OR (||)
- NOT (!)

We shall discuss each of them in more detail now.

## AND (&&) operator

The AND operation is denoted by two ‘ampersand’ symbol. To understand the AND operation, consider the following expression,

```
A && B
Where A = (3 < 4) and B = (10 < 20)
```

The final output of the expression depends on the following conditions.

A is **true** && B is **true** = A && B is **true**

A is **false** && B is **true** = A && B is **false**.

A is **true** && B is **false** = A && B is **false**.

A is **false** && B is **false** = A && B is **false**.

So, the AND is only **true** when both A and B are **true**.

## OR (||) operator

The OR operation is also useful logical operator in the C language. The value of the expression is true under the following conditions.

A is **true** || B is **true** = A || B is **true**

A is **false** || B is **true** = A || B is **true**

A is **true** || B is **false** = A || B is **true**

A is **false** || B is **false** = A || B is **false**

From the above example, it is clear that the OR is only **true** when at least A or B is **true**.

## NOT (!) operator

The NOT is called the **negation** operation. It negates the truth value of the existing expression.

For example,

if A is true, then ! A is **false**

If A is **false**, then ! A is **true**.

Note:- The *!(!A) == A.*

## Example – AND, OR, NOT

```
/* C Program to demonstrate use of Logical Operators */
#include <stdio.h>
int main() {
int A, B ;
A = (5 < 10); /* True */
B = (10 < 20); /* True */
printf("\n\n\n\n\n");
/* Working of AND operation - both A and B must be true */
if(A && B) {
printf("\tOutput AND - both A and B are true\n\n");
}
/* Operation of Not operator */
A = (2 < 10); /* False */
B = (4 < 8); /* True */
/* A && B is False */
if(!(A && B)) {
printf("\tOutput NOT - (A && B) is False so negation !(A && B) is True\n\n");
}
/* Working of OR operator - any one of A or B must be true */
if (A || B) {
printf("\tOutput OR - Any one of A or B is true\n\n");
}
getch();
return 0;
}
```

## Output – AND, OR, NOT

### References

- Balagurusamy, E. 2000.
*Programming in ANSI C.*Tata McGraw-Hill Education, - Brian W. Kernighan, Dennis M. Ritchie. 1988.
*C Programming Language, 2nd Edition.*Prentice Hall. - Kanetkar, Yashavant. 20 November 2002.
*Let us C.*Bpb Publications.

Bestseller

#### The C Programming Language – 2nd Edition

The Second Edition of The C Programming Language The book enumerates the concepts related to C Programming language, as prescribed by ANSI standard. The book elaborates on the basics of procedure oriented programming and the fundamentals of writing C codes. It goes on to cover aspects like functions and loops, learning which will help novice programmers to code elementary level codes properly. Solved and unsolved questions have been provided to help programmers develop an expertise in writing C codes flawlessly.