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 logical operator is true then 1 is returned otherwise 0 is returned. C language has 3 logical operators.

There is three type of logical operators:

Operator Meaning
&& AND
|| OR
! NOT

&& and || are binary operator while ! is a unary operator.

Before we start explaining && operator, always remember in C non-zero value is considered as true, and 0 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:

int a = 12, b = 3;

Suppose we have following logical expression:

(a==12) && (b<5)

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 used in the above expression to improve readability. So the expression:

(a<12) || (b<5)

is same as

a < 12 || b < 5

Certainly, you will agree that the former expression is much more readable than the latter one.

Consider some more examples:

Expression Intermediate Exression Result Result
(a==4) && (b==2) false && false => false 0
(a>10) && (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 && operator.

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

Consider the following example:

int a = 12;
(a==11) && (a++);

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

#include<stdio.h>

int main()
{
    int a = 12, result;

    printf("Initial value of a = %d\n", a);

    // result of the logical expression is stored in result
    result = (a==11) && (a++); 

    printf("Final value of a = %d\n", a);
    printf("Result of logical expression = %d\n", result);

    // Signal to operating system everything works fine
    return 0;
}

Expected Output:

Initial value of a = 12
Final value of a = 12
Result of logical expression = 0

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:

int a = 10, b = 9;

Suppose we have following logical expression:

(a<12) || (b<5)

In the above expression a < 12 is true while b < 5 false. Therefore the whole expression evaluates to true and the value of the whole 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: parentheses are used in above two expression to improve readability, certainly expression.

(a==4) && (b==2) is much better 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:

int a = 10;
(a==10) || (a--);

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:

#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);


    // Signal to operating system everything works fine
    return 0;
}

Expected Output:

Initial value of a = 10
Final value of a = 10
Result of logical expression = 1

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

!(NOT) operator #

! (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, ! (NOT) is unary.

Operand Result
true false
false true

Let's take an example:

int a = 10, b = 9;

Suppose we have following logical expression.

!(a>5)

As we can see a > 5 is true. And !(NOT) operator negates the value of the condition so the result 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 statements and loops , which will be discussed in later chapters in detail.

The following program demonstrate how to use NOT(!) logical operator.

#include<stdio.h>

int main()
{
    int a = 100, 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;
}

Expected Output:

Initial value of a = 100
Is a > 10 : 1
After applying not operator
Is a > 10 : 0