Logical operators in C language ( &&, ||, and ! ) usage and Examples
Introduction:
In the previous blog post, we discussed Relational operators in C Language. In today’s article, we are going to look at the Logical Operators in C programming.
Logical Operators in C Language:
The Relational Operators are useful to find the relations between the two expressions, numbers, etc.
But Sometimes there will be the need to combine or check multiple relations at the same time.
For example, You want to check if the given number ( x) is between 10 and 20.
None of the relational operators alone can achieve the above requirement. We need to combine two relation operators like
x > 10 and x < 20
To Combine the two relational operators, We use the Logical operators ( Except Not operator).
Here we want both relations x>20 and x<20 to be True. In such cases, we use Logical AND Operator.
C language supports three logical operators, They are
- Logical AND Operator ( && )
- Logical OR Operator ( || )
- Logical NOT Operator ( ! )
📢 C Langauge Logical operators return 0 for False and 1 for True. The Operand for Logical operator may be Constant, Variable, or Expression.
Logical Operators output is boolean ( Either True or False )
All Non-Zero values are considered as the True values in Logical Operations. Ex: -20, 50, 33, etc
Logical AND Operator ( && ) in C Language:
The Logical AND Operator works on two operands. This means Logical AND Operator needs two input variables. Depending upon inputs, it will provide the output.
The Logical AND Operator is denoted by && symbol in the C programming.
Syntax of Logical Operator:
1 |
Operand_2 && Operand_2 |
If both operands ( Input values ) are Non-zero then only Logical AND output will be TRUE. In all other cases, it will return False.
Logical AND Truth table:
Here is the truth table of the Logical AND Operator
A | B | A && B |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Example Program to Understand Logical AND && operator :
We are going to print the above truth table using the C language. As discussed Logical AND operator output will be True or One, Only if the two input values are true or non-zero.
1 2 3 4 5 6 7 8 9 10 |
#include<stdio.h> int main() { printf("Logical AND Operator Result : \n"); printf(" 0 && 0 = %d \n", 0 && 0 ); printf(" 1 && 0 = %d \n", 1 && 0 ); printf(" 0 && 1 = %d \n", 0 && 1 ); printf(" 1 && 1 = %d \n", 1 && 1 ); return 0; } |
Program Output:
1 2 3 4 5 |
Logical AND Operator Result : 0 && 0 = 0 1 && 0 = 0 0 && 1 = 0 1 && 1 = 1 |
Logical OR Operator ( || ) in C Langauge with Examples:
The Logical OR Operator also operates on the Two Operands or Input values.
Logical OR Operator is denoted by the || symbol in the c programming language.
Syntax of Logic OR Operator :
1 |
Operand_1 || Operand_2 |
As the name suggests, If any of the input operands are non-zero or true, then the Logical OR output will be True.
In other words, If both Operands are zero or False, Then only the output of the Logical OR will be False ( Zero )
Truth Table of Logical OR Operator:
A | B | A || B |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Example Program to understand Logical OR ( || ) Operator:
Here is a program to display the Logical OR output for various input values.
In order to get the logical OR output as True, One of the input values must need to be non-zero or True.
1 2 3 4 5 6 7 8 9 10 |
#include<stdio.h> int main() { printf("Logical OR Operator Results : \n"); printf(" 0 || 0 = %d \n", 0||0 ); printf(" 1 || 0 = %d \n", 1||0 ); printf(" 0 || 1 = %d \n", 0||1 ); printf(" 1 || 1 = %d \n", 1||1 ); return 0; } |
Program Output:
1 2 3 4 5 |
Logical OR Operator Results : 0 || 0 = 0 1 || 0 = 1 0 || 1 = 1 1 || 1 = 1 |
The input values need not be the Zeros (0) and Ones(1). We can apply the Logical Operators on any values.
Logical OR Example on non-zero values:
1 2 3 4 5 6 7 8 9 10 |
#include<stdio.h> int main() { printf("Logical OR Operator Non-Zero : \n"); printf(" 50 || 0 = %d \n", 0||0 ); printf(" 0 || -44 = %d \n", 1||0 ); printf(" -100 || 431 = %d \n", 0||1 ); printf(" 0 || 0 = %d \n", 1||1 ); return 0; } |
Program Output:
1 2 3 4 5 |
Logical OR Operator Non-Zero : 50 || 0 = 0 0 || -44 = 1 -100 || 431 = 1 0 || 0 = 1 |
Logical Operators treat non-zero values as True values. Even the Negative Integer values are also considered True. Only the value Zero is False.
📢 All Non-Zero values including Negative values are considered as True
Logical NOT ( ! ) Operator in C Programming:
The Logical NOT Operator works on one Operand ( One Input value)
Logical NOT Operator gives us the Negation of Input Value.
If the input Operand value is Non-Zero, Then Logical NOT Operator output will be Zero.
Similarly, If the Input Operand value is Zero, Then the output will be True.
Logical NOT Operator is denoted by an Exclamation mark ( ! )
Truth Table of Logical NOT:
A | !A |
---|---|
1 | 0 |
0 | 1 |
As you can see, The NOT operator is giving the opposite value as the output.
Program to Understand the Logical NOT operator:
1 2 3 4 5 6 7 8 |
#include <stdio.h> int main(void) { printf(" Logical NOT Operator Result : \n"); printf(" !1 is : %d \n", !1 ); printf(" !0 is : %d \n", !0 ); return 0; } |
Program Output:
1 2 3 |
Logical NOT Operator Result : !1 is : 0 !0 is : 1 |
As we discussed earlier, All Non-Zero values are True, So applying the Logical NOT operator on all non-zero values results into the False
Program Logical NOT on Non-zero Values:
1 2 3 4 5 6 7 8 9 |
#include <stdio.h> int main(void) { printf(" Logical NOT Operator Result : \n"); printf(" !77 is : %d \n", !1 ); printf(" !-23 is : %d \n", !0 ); printf(" !0 is : %d \n", !0 ); return 0; } |
Output:
1 2 3 4 |
Logical NOT Operator Result : !77 is : 0 !-23 is : 1 !0 is : 1 |
Conclusion:
In this article, We have looked at the Logica Operators in C Language and different types of Logical operators with example programs.
Tutorials Index:
- Start Here – Step by step tutorials to Learn C programming Online with Example Programs – SillyCodes
Related Articles:
- Arithmetic Operators with Examples.
- Arithmetic operators priority and it’s Associativity.
- Modulus Operator and Hidden Concepts of Modulus Operator.
- Precedence Table or Operators Priority Table.
- Assignment Operator, Usage, Examples
- Increment Operator and Different types of Increment operators Usage with Examples.
- Decrement Operator and Different types of Decrement operators with Examples.
9 Responses
[…] Logical Operators […]
[…] Logical Operators […]
[…] our previous article, We discussed the Logical Operators in C, In today’s article, We will look at the Conditional Operator in C Langauge. Conditional […]
[…] our previous articles, We have discussed the Logical Operators and Conditional operators in C Language. In today’s article, We are going to look at the […]
[…] ➡️ Learn More about Logical Operators Here – Logical operators in C language ( &&, ||, and ! ) usage and Examples (sillycodes.com) […]
[…] 📢 Learn More about Logical Operators Here – Logical operators in C language ( &&, ||, and ! ) usage and Examples (sillycodes.com) […]
[…] Logical Operators […]
[…] Logical Operators […]
[…] Logical Operators […]