# Logical Operators in C Programming

By BYJU'S Exam Prep

Updated on: September 25th, 2023

The C programming language includes three logical operators that can be used to combine the results of two or more logical expressions, conditions, or boolean values. Two of the three logical operators in C are binary in nature, while the third is unary. These logical operators can be used to create complex logical expressions and conditionals in C programming to control the flow of execution based on certain conditions.

Logical Operators in C PDF [GATE Notes]

In C, logical operators are used to combine or modify the logical values of operands. This article will cover logical operators in C in detail. We will look at the use of logical operators and learn how different logical operators in C, such as AND, OR, and NOT work.

Table of content

## Logical Operators in C

Logical operators in C join two or more expressions or conditions to perform logical operations on a given expression. It is applicable in a variety of relational and conditional expressions. This operator uses Boolean values to check the condition logically and returns 1 if the conditions are true.

The logical operators in C return 0 if the conditions are false. There are three types of logical operators in C programming that are as follows:

• Logical AND(&&) operator
• Logical OR(||) operator
• Logical NOT(!) operator

## Logical AND Operator in C

A binary operator is the logical AND. It combines two inputs into one. The result is true only if both inputs are true and false if one of the inputs is false. The truth table of the logical operators in C is as follows:

 Input 1 Input2 Output 0 0 0 0 1 0 1 0 0 1 1 1

## Logical OR (||) Operator

A binary operator is the logical OR. It combines two inputs into one. The result is false only if both inputs are false and true if one of the inputs is true. The truth table of the logical operators in C is as follows:

 Input 1 Input2 Output 0 0 0 0 1 1 1 0 1 1 1 1

## Logical NOT (!) Operator

The logical NOT operator is the only unary operator among the logical operators. This will take one input and return its complement as the output. If the input is true, the output is false, and if the input is false, the output is true.

 Input Output 0 1 1 0

## Short-Circuiting Property in Logical Operators in C

The logical AND and logical OR operators perform short-circuiting operations, but the logical NOT operator not be used for this purpose. Short-circuiting is skipping sections of code to improve a program’s efficiency.

If you want to understand the short-circuit property in logical operators in C and improve the efficiency of your program, keep these two points in mind.

When the first input is false, the logical AND operator does not evaluate the second.

The logical OR operator does not evaluate the second input when the first input is true.

## Logical Operators in C Examples

The program given below is an example of the logical operators in C that are as follows:

#include
int main()

{

int a = 2, b = 2, c = 4, result;

result = (a == b) && (c < b);
printf((a == b) && (c < b) is %d \n, result);

result = (a == b) && (c > b);
printf((a == b) && (c > b) is %d \n, result);

result = (a == b) || (c < b);
printf((a == b) || (c < b) is %d \n, result);

result = (a != b) || (c < b);
printf((a != b) || (c < b) is %d \n, result);

result = !(a == b);
printf(!(a == b) is %d \n, result);

result = !(a != b);
printf(!(a != b) is %d \n, result);

return 0;

}

Output:

(a == b) && (c < b) is 0
(a == b) && (c > b) is 1
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a == b) is 0
!(a != b) is 1

Explanation:

(a == b) && (c < b) returns 0 because operand (c < b) is 0(false).

(a == b) && (c > b) = 1 because both operands (a == b) and (c > b) are 1(true).

(a == b) || (c < b) equals 1 because (a == b) equals 1. (true).

(a!= b) || (c < b) returns 0 because both operands (a!= b) and (c < b) is 0(false).

Because (a == b) is 1, it evaluates to 0. (true). As a result,!(a == b) equals 0. (false).

Because operand (a!= b) is 0(false). As a result,!(a!= b) equals 1(true).

## Problems on Logical Operators in C

Problem 1: Consider the following code:

#include

int main()

{

int x = 7, y = 3, z = 3;
int d;
d = z + y == x;
printf(\%d, d);

}

What is the output of the code?

1. 0
2. 1
3. 6
4. 7

Problem 2: What will be the output of the following C code?

#include
int main()

{

int x = 10, y = 5, z = 3;
y != !x;
z = !!x;
printf(\%d \t %d, y, z);

}

1. 5 1
2. 0 3
3. 5 3
4. 1 1

Problem 3: Consider the code:

#include
int main()

{

int a = 10;
if (a == a–)
printf(True 1\t);
a = 10;
if (a == –a)
printf(True 2\t);

}

What will be the output of the following C code?

1. True 1
2. True 2
3. True 1  True 2
4. Compiler Dependent