Logical Operators In C [Full Information With Examples]
Logical Operators In C
Logical Operators are used to combine two and more conditions. The result always gets Boolean value by Logical Operators, which means the result is given as True or False and we consider True as 1 and False as 0.
For Example -: In C language Logical Operators (&&) returns true in the result when both conditions are satisfied and if both conditions are not satisfied then it returns False in the result.
Examples of Logical Operators are : (&&, ||,!) In this, “&&” is called AND Operator, ” || ” is called OR Operator. ” ! ” is called NOT Operator.
Let’s now learn about all these operators one by one.
Logical AND operator in C Language
The Logical AND operator (&&) operator is used to check any two conditions.
This operator checks both the given conditions whether those conditions are correct or not. If both the conditions are true then the ‘&&’ operator returns true in the result and if the conditions are not true then it returns false.
Let’s understand the same thing in the form of a table.
Operand 1 | Operand 2 | Result |
true | true | true |
true | false | false |
false | true | false |
false | false | false |
Let’s see an example:
int a = 10, b = 6;
Suppose we have the following logical expression :
(a == 10) && (b <10)
Both the conditions a == 10 and b <10 are true in the above expression, so the Logical AND operator will return 1 or True in the result.
Let’s see some more examples of Logical AND operator
Expression | Intermediate Expression | Result |
(a==4) && (b==2) | false && false => false | 0 |
(a>100) && (b<10) | false && true => false | 0 |
a && b | true && true => true | 1 |
a && 0 | true && false => false | 0 |
Consider the last two examples, in this, we have used variables and constants as operands. In operands with logical operators, we can also use variables and constants with expressions and this is the most important thing about the && operator.
In the Logical AND operator (&&), if the first operand is False, then the second operand is also not checked.
If there is a single operand False in AND operator (&&), then this operator always returns False in the result.
Let’s understand this by an example:
int a = 10;
(a == 21)&&(a++);
In the above expression, a == 21 is false, so operand a++ is not checked even if that second operand is true or false. Whatever the second operand, direct results are given.
#include <stdio.h>
void main ()
{
int a = 10, result;
printf ("Initial value of a is: %d \n", a);
// result of the logical expression is stored in result
result = (a == 20) && (a++);
printf ("Final value of a is: %d \n", a);
printf ("Result of logical expression is: %d \n", result);
}
Output -:
Initial value of a is: 10
Final value of a is: 10
Result of logical expression is: 0
You saw in this example, a == 20 is wrong in the expression but the operand a++ was correct, its result should be 11, but the compiler did not check it because the first operand was wrong and that is why the compiler after the first operand was wrong Did not run a++ at all.
Let’s now see what the result would have been if the first operand had been correct.
#include <stdio.h>
void main ()
{
int a = 10, result;
printf ("Initial value of a is a: %d \n", a);
// result of the logical expression is stored in result
result = (a == 10) && (a++);
printf ("Final value of a is: %d \n", a);
printf ("Result of logical expression is: %d \n", result);
}
Output -:
Initial value of a is a: 10
Final value of a is: 11
Result of logical expression is: 1
See, there was no increment in the value of an in the first result because the compiler had not run it after the first operand went wrong and now the result which has come has an increment in the value of a because after the first operand is corrected. The compiler also ran the second operands, which resulted in an increase in the value of a.
Do you understand what I am trying to tell you?
If yes, a comment will definitely tell you whether you have understood this or not and if there is any doubt then tell me in the comment.
Let’s now know about the Logical OR operator (||).
Logical OR operator in C Language
Out of both the given conditions, this operator returns true in the result if any one of the conditions is true and if both the conditions are not true then it returns false.
Operand 1 | Operand 2 | Result |
true | true | true |
true | false | true |
false | true | true |
false | false | false |
Let’s see an example
int a = 10, b = 9;
Suppose we have the following logical expression:
(a <12) || (b <5)
The first condition in the above expression, a <12 is correct, so the Logical or operator will not even check the second condition, b <5 which is false and will return 1/True directly in the result.
Logical or operator returns true in the result when a single condition is true and in this example only the first condition was true, so it returned the result true without checking the second condition.
Let’s see some more examples of Logical Or operator.
Expression | Intermediate Expression | Result |
(a==4) || (b==2) | false || false => false | 0 |
(a>10) || (b<10) | false || true => true | 1 |
a || b | true || true => true | 1 |
a || 12.12 | true || true => true | 1 |
Note – the Expression of the last, (a || 12.12) In this we have used double type Constant as an operand which is acceptable in Logical Or operator.
Like I told you above that if the first operand of the Logical Or operator is correct, then the Logical Or operator does not even check the second operand. Let’s understand the same thing with an example below.
#include <stdio.h>
int main ()
{
int a = 10, result;
printf ("Initial value of a = %d \n", a);
// result of the logical expression is stored in result
result = (a == 10) || (a--);
printf ("Final value of a = %d \n", a);
printf ("Result of logical expression = %d \n", result);
return 0;
}
Output -:
Initial value of a = 10
Final value of a = 10
Result of logical expression = 1
In this example, in the first result, there was no decrement in the value of a because the first operand was correct, the first operand of the Logical Or operator is correct, then the Logical Or operator does not even check the second operand.
Let’s now learn about the Logical NOT operator (!).
Logical NOT operator in C Language
Logical NOT operator (!) Returns Boolean type results. If the condition is true then the NOT operator (!) Makes it false and returns false in the result, and if the condition is false then the NOT operator (!) Returns it in the result by making it true.
Operand | Result |
true | false |
false | true |
Let’s understand this better with the help of a program -:
int a = 11, b = 7;
logical expression
! (a> 5)
In this example (a> 5) is correct but NOT operator (!) Returns False in the result because it is the Behavior.
Example -:
#include <stdio.h>
int main ()
{
int a = 110, result;
printf ("Initial value of a = %d \n", a);
// result of the logical expression is stored in result
result = (a>10);
printf ("Is a>10 : %d \n", result);
printf ("After Applying not operator \n");
printf ("Is a>10 : %d \n", !result);
// Signal to operating system everything works fine
return 0;
}
Output -:
Initial value of a = 110
Is a>10 : 1
After applying not operator
Is a>10 : 0
Read More -:
- What is Operators In C
- Relational Operators In C
- Arithmetic Operators In C
- Bitwise Operators In C
- Assignment Operators In C
- Conditional Operator in C
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
Friends, I hope you have found the answer of your question and you will not have to search about the Logical Operators in C Language
However, if you want any information related to this post or related to programming language, computer science, then comment below I will clear your all doubts.
If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the Logical Operators in C Language
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.