Logical Operators in C

Logical operators in C evaluate to either True or False. Logical operators are typically used with Boolean operands.

The logical AND operator (&&) and the logical OR operator (||) are both binary in nature (require two operands). The logical NOT operator (!) is a unary operator.

Since C treats “0” as False and any non-zero number as True, any operand to a logical operand is converted to a Boolean data.

Here is a table showing the logical operators in C −

OperatorDescriptionExample
&&Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.(A && B)
||Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true.(A || B)
!Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.!(A)

The result of a logical operator follows the principle of Boolean algebra. The logical operators follow the following truth tables.

Logical AND (&&) Operator

The && operator in C acts as the logical AND operator. It has the following truth table −

aba&&b
truetrueTrue
truefalseFalse
falsetrueFalse
falsefalseFalse

The above truth table shows that the result of && is True only if both the operands are True.

Logical OR (||) Operator

C uses the double pipe symbol (||) as the logical OR operator. It has the following truth table −

aba||b
truetrueTrue
truefalseTrue
falsetruetrue
falsefalsefalse

The above truth table shows that the result of || operator is True when either of the operands is True, and False if both operands are false.

Logical NOT (!) Operator

The logical NOT ! operator negates the value of a Boolean operand. True becomes False, and False becomes True. Here is its truth table −

A!a
TrueFalse
FalseTrue

Unlike the other two logical operators && and ||, the logical NOT operator ! is a unary operator.

Example 1

The following example shows the usage of logical operators in C −

#include <stdio.h>intmain(){int a =5;int b =20;if(a && b){printf("Line 1 - Condition is true\n");}if(a || b){printf("Line 2 - Condition is true\n");}/* lets change the value of  a and b */
   a =0;
   b =10;if(a && b){printf("Line 3 - Condition is true\n");}else{printf("Line 3 - Condition is not true\n");}if(!(a && b)){printf("Line 4 - Condition is true\n");}return0;}

Output

Run the code and check its output −

Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true

Example 2

In C, a char type is a subset of int type. Hence, logical operators can work with char type too.

#include <stdio.h>intmain(){char a ='a';char b ='\0';// Null characterif(a && b){printf("Line 1 - Condition is true\n");}if(a || b){printf("Line 2 - Condition is true\n");}return0;}

Output

Run the code and check its output −

Line 2 - Condition is true

Logical operators are generally used to build a compound boolean expression. Along with relational operators, logical operators too are used in decision-control and looping statements in C.

Example 3

The following example shows a compound Boolean expression in a C program −

#include <stdio.h>intmain(){int phy =50;int maths =60;if(phy <50|| maths <50){printf("Result:Fail");}else{printf("Result:Pass");}return0;}

Output

Result:Pass

Example 4

The similar logic can also be expressed using the && operator as follows −

#include <stdio.h>intmain(){int phy =50;int maths =60;if(phy >=50&& maths >=50){printf("Result: Pass");}else{printf("Result: Fail");}return0;}

Output

Run the code and check its output −

Result: Pass

Example 5

The following C code employs the NOT operator in a while loop −

#include <stdio.h>intmain(){int i =0;while(!(i >5)){printf("i = %d\n", i);
      i++;}return0;}

Output

In the above code, the while loop continues to iterate till the expression “!(i > 5)” becomes false, which will be when the value of “i” becomes more than 5.

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5

C has bitwise counterparts of the logical operators such as bitwise AND (&), bitwise OR (|), and binary NOT or complement (~) operator.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *