C Logical Operators logical operators in c

C Logical Operators

Many times in C programming we need to generate logical expressions by combining two or more expressions. This is possible only through C logical operators. The combination of expressions/conditions along with the logical operator is called the “Logical Expression“. The value of a logical expression will be either true (1) or false (0). Whatever expressions are combined with logical operators to generate logical expressions, also called logical operands. These logical operands can be a variable, a constant, or an expression in itself. Logical operators are also called the “Boolean operators”. C Supports three logical operators. These C logical operators are shown below.

S. No.Operator SymbolOperator Name
1.&&Logical AND
2.||Logical OR
3.!Logical NOT

Here the C logical operators “Logical AND” and “Logical OR” are the binary operators while the “Logical NOT” is a unary operator. In C programming, if any condition returns a nonzero value, then that condition is always considered to be true. Otherwise, the condition will always be false.

Logical AND Operator

If both expressions/conditions that have been combined through this operator are true, then the value of the Logical Expression will also be true. Otherwise, the value of the Logical Expression will be false.

Value Of Condition 1Value Of Condition 2Value Of Logical Expression
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

Let u and v are two variables with their respective values as u=2, v=5. Now the “AND” logical operator will work like:

Logical ExpressionManipulationResultValue
(u!=7) && (v==5)(True) && (True)True1
(v>u) && (u>=v)(True) && (False)False0
(v<u) && (u)(False) && (True)False0
(v==u) && (u==7)(False) && (False)False0

Logical OR Operator

If both expressions/conditions that have been combined through this operator are false, then the value of the Logical Expression will always be false. Otherwise, the value of the Logical Expression will be true.

Value Of Condition 1Value Of Condition 2Value Of Logical Expression
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Let u and v are two variables with their respective values as u=2, v=5. Now the “OR” logical operator will work like:

Logical ExpressionManipulationResultValue
(u!=7) || (v==5)(True) || (True)True1
(v>u) || (u>=v)(True) || (False)True1
(v<u) || (u)(False) || (True)True1
(v==u) || (u==7)(False) || (False)False0

Logical NOT Operator

It is a Unary operator and it negates the value of the expression. if the value of the expression is true (1), then it makes it false (0). Otherwise, it produces the true (1) value, if the given value of the expression is false (0).

Value Of ConditionValue Of Logical Expression
TrueFalse
FalseTrue

Let u and v are two variables with their respective values as u=2, v=5. Now the “NOT” logical operator will work like:

Logical ExpressionManipulationResultValue
! (v>u)! (True)False0
! (v<u)! (False)True1
Example:
#include<stdio.h>
main()
{
int u=5, v=2;
if(u>v && u!=0)
{
printf("Hurray both conditions are true\n");
}
if(v>u || v!=7)
{
printf("Oh great one condition is true\n");
}
if(!(u!=v && v>7))
{
printf("The final result is True indeed\n");
}
}
Output:
Hurray both conditions are true
Oh great one condition is true
The final result is True indeed

In this tutorial, we learned about C logical operators and now in the next tutorial we will learn about the “conditional operators in C programming”.

Leave a Comment

Your email address will not be published. Required fields are marked *