Tutorial Playlist
Logical Operators in C are used to perform logical operations on a given logical expression. They are unary or binary operators, depending on the number of operands (conditions) they act upon, and evaluate the expression to 1 (True) or 0 (False). They are used for decision-making, often in conditional statements.
Let’s dive in to grasp a deeper insight into the world of logical operators and their implementations.
In C programming language, we have three logical operators:
We will see the function of each of these logical operators in detail and understand their working with the help of their Truth Table. We will also see example snippets for each of them, which helps us understand their usage in C.
Logical NOT (!) operator
The logical NOT operator, represented as ‘!’, is a unary logical operator in C. It is also called a negation operator, which means it takes the input operand and negates it. If the input is 1 (True), the result will be 0 (False). If the input is 0 (False), the result will be 1 (True).
Truth Table for Logical NOT operator
X | ! (X) |
0 | 1 |
1 | 0 |
Logical AND (&&) Operator
The logical AND operator, represented as ‘&&’, is a binary logical operator in C. It takes two input operands and evaluates the result as True if both the input operands are True. It returns False in every other scenario.
Truth table for Logical AND Operator
X | Y | X&&Y |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Logical OR (||) Operator
The logical OR operator, represented by ‘||’, is a binary logical operator in C. It takes two input operands and evaluates the result as True if at least one of them is True. It returns False only if both the input operands are False.
Truth table for Logical OR Operator
X | Y | X||Y |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Logical NOT (!) Operator
Syntax:
!(condition) |
Example:
// C program to illustrate Logical NOT operator
#include<stdio.h> |
Output:
Please sign in to continue.. |
Logical AND (&&) Operator
Syntax:
(condition1 && condition2) |
Example:
// C program to illustrate Logical AND operator
#include<stdio.h> |
Output:
8 and 10 are both even numbers |
Logical OR (||) Operator
Syntax:
(condition1 || condition2) |
Example:
// C program to illustrate Logical OR operator
#include<stdio.h> |
Output:
Thank You for playing |
In some scenarios, the C compiler skips the evaluation of parts of a logical expression when it is able to determine the result without fully evaluating it. This is known as Short-circuiting in C. This kind of behaviour can be observed while evaluating the logical AND & logical OR operators. Let us take a look at a few examples.
// C program to illustrate short-circuiting with Logical OR operator
#include<stdio.h> |
Output:
The condition evaluates to True |
In the above code, we are using the logical OR condition with two operands 1 and 0. We know that logical OR evaluates to True if any one of the input operands is True. With 1 being the first operand, the C compiler is able to optimise the execution and skip evaluating the second operand 0, as this value is immaterial to the final result of the expression.
This scenario can be better understood with the below example, where the value of the variable is being updated within the condition
// C program to illustrate short-circuiting with Logical OR operator
#include<stdio.h> |
Output:
The condition evaluates to True |
In the above example, we are decrementing the value of variable ‘a’ and incrementing the value of variable ‘b’ within the conditional statement. The first condition, --a > 1 decrements the value of ‘a’ to 2 and compares 2 > 1, which returns True. As OR operator evaluates to True if at least one of the operands is True, the second condition ++b > 2 need not be evaluated. This can be seen in the final print of the output, where the value of ‘a’ is decremented, but the value of ‘b’ remains unchanged.
Let us take a look at a similar code but with the first condition evaluating to False so that there is no short-circuit taking place
// C program to illustrate short-circuiting with Logical OR operator
#include<stdio.h> |
Output:
The condition evaluates to True |
The above code snippet is similar to the previous one except that the first condition evaluates to False. This causes the compiler to evaluate the second condition as well, which is True and hence the final result is True. The final print shows that both the variables’ values are updated.
Let us also look at one final short-circuit scenario for the logical AND operator
// C program to illustrate short-circuiting with Logical AND operator
#include<stdio.h> |
Output:
The condition evaluates to False |
In C, all logical operators have left-to-right associativity, meaning that when multiple operands are given as input to the same logical operator, the order of evaluation is from left to right. For example, consider the logical expression a || b || c, first, the leftmost operands a || b is evaluated, and the result of this is chained to the next operand, i.e. ( (a || b) || c ).
The precedence of the logical operators is the order in which each operator is executed. These are crucial to understand when multiple logical operators are being used in a single logical expression. The order from highest to lease precedence is:
1. Logical NOT (!) operator
2. Logical AND (&&) operator
3. Logical OR (||) operator
In order to revise all the logical operator concepts that we’ve been through so far, let’s explore a few practice problems! The output for each of the questions is given below:
// C practice programs
#include<stdio.h> |
Output:
Q1: The condition is True |
This tutorial has covered the basics of logical operators in C, along with various examples and practice problems to test your understanding. With the right resources, learning material, and a decent amount of practice, you can master the C programming language.
Keep exploring and going through more C programs to enhance your skills!
While practising using online material is an excellent way to strengthen your programming skills, higher education can be a great addition to your skill set.
Check out upGrad’s Master of Science in Computer Science offered by Liverpool John Moores University, a globally recognised university with guidance from industry experts and leading faculties. The course allows you to specialise in Software Development using languages like Java & Python. Throughout the course, students are provided with extensive support through video tutorials, case studies, real projects and live sessions, helping to gain a competitive edge!
1. What happens when the value of a logical expression is assigned to a variable?
A logical expression returns integer 0 to indicate False and a non-zero value (1) to indicate True.
2. What is the precedence of logical operators in C?
The precedence of logical operators is as follows:
Logical NOT > Logical AND > Logical OR.
3. What is the Truth Table for a logical operator?
The Truth Table of a logical operator helps us understand the operator's working. It gives us the result for all possible combinations of input operands.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...