Understanding Logical Operators in C
In this article, we will learn the logical operators and how to implement them in C, with examples. Let’s begin.
In C programming, a logical operator is a symbol or keyword used to perform a logical operation, such as AND, OR, NOT, etc. These operators are used to compare values and evaluate expressions. Additionally, logical operators in C programming are used in conditional statements, such as if-else statements or while loops, to control the flow of a program based on the outcome of a logical comparison.
Also, Explore Data Types in C.
Check out: C Programming Online Courses & Certifications
We will be covering the following sections:
Explore programming courses
So, without further ado, let’s get started!
Must read: Operators in C programming
Logical Operators in C
C programming language has three operators that are used to combine the results of multiple logical expressions or conditions. These operators are:
- Logical AND
- Logical OR
- Logical NOT
Must check: Top 10 Programming Languages to Learn in 2023
These operators are commonly used in control flow statements such as if-else or while loops to determine the execution of a program based on the outcome of logical comparisons.
Operator | Symbol | Description |
Logical AND | && | Returns true if both operands are true, otherwise returns false |
Logical OR | || | Returns true if at least one of the operands is true, otherwise returns false |
Logical NOT | ! | Inverts the value of the operand, returns true if the operand is false, and vice versa |
Note: In C, True is represented by any non-zero value and False is represented by 0.
Let’s discuss each of the three operators in detail.
Yoy can also explore: Understanding Data Structures in C: Types And Operations
Logical AND Operators in C
The logical AND operator is a binary operator that takes two inputs. It will only return a True if both of the inputs (operands) are true. If one of the inputs is false, it will return a False regardless of the other input.
The truth table for the logical AND operator is as follows:
A | B | A && B |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
In this table, A and B are the operands, and A && B is the result of the logical AND operation. The logical AND operator (&&) returns true (1) only if both operands are true (1). If either operand is false (0), the result is false (0).
Bottom of Form
Let us consider an example in C:
#include <stdio.h>
int main() { int x = 5; int y = 10;
if (x > 0 && y > 0) { printf("Both x and y are greater than 0\n"); } else { printf("Either x or y or both are not greater than 0\n"); } return 0;}
Output:
Both x and y are greater than 0.
In this example, the program uses the logical AND operator (&&) to check if the values of x and y are both greater than 0. If both x and y are greater than 0, the program will print “Both x and y are greater than 0”. If either x or y (or both) are not greater than 0, the program will print “Either x or y or both are not greater than 0”.
Logical OR Operator
The logical OR operator is a binary operator that takes two inputs. It will return True if either of the inputs (operands) is true. Only if both the inputs are false, will it return False.
The truth table for the logical OR operator is as follows:
A | B | A || B |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
In this table, A and B are the operands, and A || B is the result of the logical OR operation. The logical OR operator (||) returns true (1) if either operand is true (1). If both operands are false (0), the result is false (0).
Bottom of Form
Let us consider an example in C:
#include <stdio.h>
int main() { int x = 5; int y = -2;
if (x > 0 || y > 0) { printf("Either x or y or both are greater than 0\n"); } else { printf("Both x and y are not greater than 0\n"); } return 0;}
Output:
Either x or y or both are greater than 0.
In this example, the program uses the logical OR operator (||) to check if the values of x and y are either greater than 0. If either x or y is greater than 0, the program will print “Either x or y or both are greater than 0”. If both x and y are not greater than 0, the program will print “Both x and y are not greater than 0”.
Logical NOT Operator
The logical NOT operator is a unary operator that takes only one input. If the input is true, it will return False and vice-versa.
The truth table for the logical NOT operator is as follows:
A | !A | |
0 | 1 | |
1 | 0 | |
In this table, A is the operand, and !A is the result of the logical NOT operation. The logical NOT operator (!) negates the value of the operand. If the operand is true (1), the result is false (0). If the operand is false (0), the result is true (1).
Bottom of Form
Let us consider an example in C:
#include <stdio.h>
int main() { int x = 5;
if (!(x > 0)) { printf("x is not greater than 0\n"); } else { printf("x is greater than 0\n"); } return 0;}
Output:
x is greater than 0.
In this example, the program uses the logical NOT operator (!) to check if the value of x is not greater than 0. If x is not greater than 0, the program will print “x is not greater than 0”. If x is greater than 0, the program will print “x is greater than 0”. It’s important to notice that the NOT operator is used to negate the expression inside the parentheses, so if the x > 0 is true, the negation of it will be false and vice versa.
Precedence of Logical Operators
In C programming, the order in which logical operators are evaluated is determined by their precedence. The precedence of logical operators is as follows:
- NOT (!)
- AND (&&)
- OR (||)
This means that the NOT operator has the highest precedence and is evaluated first, followed by the AND operator, and finally the OR operator.
When multiple logical operators are used in a single expression, the one with the highest precedence is evaluated first, and the result is then used as an operand for the next operator with a lower precedence. If there is any confusion, you can use parentheses to specify the order of evaluation.
For example, in the expression:
(A && B) || (!C && D)
The logical AND operator (&&) has higher precedence than the logical OR operator (||), so the expression inside the first set of parentheses (A && B) will be evaluated first. Then the logical NOT operator (!C) has higher precedence than the logical AND operator (&&) so the expression inside the second set of parentheses will be evaluated next, and finally the logical OR operator (||) will be evaluated last.
Explore: Top 80+ C Programming Interview Questions and Answers
Endnotes
Hope this article was helpful for you to understand logical operators in C Programming. We learned how these operators allow you to combine the results of two or more logical expressions or conditions.
Explore our C articles to find out more about the language and consolidate your knowledge of the fundamentals.
Contributed By: Prerna Singh
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio