Logical Operators in C

Logical operators are used to evaluate two or more conditions. In General, Logical operators are used to combine relational expressions, but they are not limited to just relational expression you can use any kind of expression even constants. If the result of the logical operator is true then 1 is returned otherwise 0 is returned.

There are three types of logical operators:

Operator Meaning
&& AND operator
|| OR operator
! NOT operator

The AND ( && ) and OR ( || ) are binary operator while NOT ( ! ) is a unary operator.

Before we start explaining && operator, keep in mind that – In C, all non-zero values are considered as true ( 1 ) while 0 is considered as false.

AND(&&) operator

This operator gives the net result of true (i.e 1) if both operands are true, otherwise false (i.e 0).

Operand 1 Operand 2 Result
true true true
true false false
false true false
false false false

Let’s take an example:

Suppose we have the following logical expression:

In the above expression both the conditions a == 12 and b < 5 are true, therefore the whole expression is true. As a result, the value of the whole logical expression is 1.

Parentheses are added to the above expression just for the sake of readability. It doesn’t alter the order of operation in any way. So the expression:

is equivalent to:

Certainly, the former expression is much more readable than the latter one.

Consider some more examples:

Expression Intermediate Expression Result 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

Pay special attention to the last two examples, as discussed, operands in a logical expression can be any expression, variables or constants.

Here is the most important thing about the && operator.

In && (AND) operator if the first operand evaluates to false, then the second operand in not evaluated at all.

Consider the following example:

In the above expression a==11 is false, so the right operand a++ is not evaluated at all. The following program demonstrates this concept:

Expected Output:

If the condition a==11 had been true then the value of a would have been incremented by 1.

OR(||) operator

This operator gives the net result of true (i.e 1) if at least one operand is true, otherwise false.

Operand 1 Operand 2 Result
true true true
true false true
false true true
false false false

Let’s take an example:

Suppose we have the following logical expression:

In the above expression a < 12 is true while b < 5 is false. Therefore the whole expression evaluates to true and the value of the logical expression is 1.

Consider some more examples:

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 that the parentheses are used in above two expression to improve readability, certainly the expression (a==4) && (b==2) is more readable than a==4 && b==2.

Notice that in the last statement second operand is of type double which is perfectly acceptable.

The most important thing about the OR(||) operator is that if the first operand evaluates to true, then the second operator is not evaluated.

Consider the following example:

In the above expression a==10 is true, so the expression a-- is not evaluated and the result of the overall expression is 1. The following program demonstrates this concept:

Expected Output:

If the condition a==10 had been false, then the value of a would be decremented by 1.

!(NOT) operator

The ! (not) operator negates the value of the condition. If the condition is false then it becomes true, if it is true then it becomes false. Unlike && (AND) and || (OR) operator, the ! (NOT) operator is unary.

Operand Result
true false
false true

Let’s take an example:

Suppose we have the following logical expression.

As we can see, the condition a > 5 is true. And !(NOT) operator negates the value of the condition, so the result of the overall expression is false i.e 0.

Here are some more examples:

Expression Intermediate Expression Result
!(a==4) !false => true 1
!(a || b) !true => false 0
!(a && b) !true => false 0
!(a > b) !true => false 0

Note: To use relational and logical to its full potential you must first master if-else statements and loops.  If-else statements and loop are discussed in detail in If… else statements in C and The while loop in C chapters, respectively.

The following program demonstrates the NOT(!) operator in action:

Expected Output:

Precedence

Among logical operators, the NOT (!) operator has the highest precedence and it associates from right to left. The precedence of AND (&&) operator is higher than OR (||) operator and they both associates from left to right (see the full precedence table).

Let’s now solve some expressions involving logical operators.

Example 1:

Solution:

In this case, the order of evaluation of operators will be parentheses ( () ), relational operators (<, >), AND ( && ) operator, OR ( || ) operator.

Example 2:

Solution:

In this case, the order of evaluation of operators will be parentheses ( () ), Modular division (%), Relational operators (==, !=) AND ( && ) operator, OR ( || ) operator.

1 thought on “Logical Operators in C

Leave a Comment

%d bloggers like this: