logical operators in C

In this blog post, you will learn equality and logical operators in C programming with the help of example code. So let’s first understand what logical operators are.

The logical operators perform logical operations on given expressions. They use to evaluate two or more conditions. The result of the logical operations yields 1 or 0 depending upon whether the expression results are true or false.

We use a logical operator when the evaluation of an expression depends on more than one condition.  For example, if someone wants to display a warning message when the temperature goes beyond 55°C or below 0°C, then they can combine these two conditions using the logical OR operators.

List of Logical Operators in C:

The following table contains logical operators along with some examples. The logical negation operator (!) has very high precedence than && and ||.  Also, the && operator has higher precedence than ||. The result has type int. In the below section I will discuss the precedence and Associativity in detail.

Operator Result Example
&& Result of Logical AND 1 only if both operands are true. If a = 3 and b = 7 then, expression ((a>2) && (b<15)) evaluate 1.
|| Result of Logical OR 1 only if either one operand is true. If a = 3 and b = 7 then, expression ((a>12) || (b<15)) evaluate 1.
! Result of Logical NOT 1 only if the operand is 0. If a = 0 then, the expression !c evaluates 1.

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

Note: In C programming, non-zero values are considered as true ( 1 ) while 0 is considered as false.

 

Logical AND Operator (&&):

The && operator yields 1 if both of its operands is non-zero; otherwise, it yields 0. The logical AND operator result type is int and both operands must be scalar type (“single” value like integer, boolean).

Its syntax is following,

expression_1 && expression_2

 

✍ The && operator guarantees left-to-right evaluation.

 

Flow Chart of Logical AND Operator:

Following is a flow chart of the AND Operator.

logical and operator in c

 

The && operator works as follows:

✍ The first operand (expression_1) must be a scalar type (“single” value like integer, boolean). It is evaluated first; there is a sequence point between its evaluation and the evaluation of the second operand if the second operand is evaluated.

✍ The second operand (expression_2) is evaluated only if the first operand evaluates to a non-zero ( unequal to 0) value. This short-circuit evaluation could be used to prevent null-pointer dereferencing, for example,

int *ptr = NULL;
/*
 Some code.
 Also forget to assign memory to ptr
*/

if((ptr) && (*ptr == 2))
{
 //code.
}

If ptr is a null pointer, the right side of the expression is never evaluated.

✍ Logical AND expression evaluate 1 only both operand evaluates true.

 

The following table explains the result of AND operator (&&) on two operands.

Operand 1 Operand 2 Result
true true 1
true false 0
false Not-evaluate 0

 

Examples for Logical And Operator:

The following examples illustrate the use of Logical And Operators:

Example1:

#include<stdio.h>

int main()
{
    int num = 10, ret;

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

    ret = (num > 11) && (num++);

    printf("Result of logical expression = %d\n", ret);

    printf("Final value of num = %d\n", num);

    return 0;
}

Output:

Initial value of num = 10
Result of logical expression = 0
Final value of num = 10

In logical AND expression if the first operand evaluates false, then the second operand is not evaluated. The above example code explains the same concept. The expression num >11 is false, so the right operand num++ is not evaluated.

 

Example2:

#include<stdio.h>

/*
   C Program to find the Smallest
   number using the Logical AND operator.
*/
int main()
{
    int num1, num2, num3;
    printf(" Enter the number1 = ");
    scanf("%d", &num1);
    printf("\n Enter the number2 = ");
    scanf("%d", &num2);
    printf("\n Enter the number3 = ");
    scanf("%d", &num3);

    if((num1 < num2) && (num1 < num3))
    {
        printf("\n Smallest number is %d\n", num1);
    }
    else if ((num2 < num1) && (num2 < num3))
    {
        printf("\n Smallest number is %d\n", num2);
    }
    else
    {
        printf("\n Smallest number is %d\n", num3);
    }

    return 0;
}

Output:

Example code on logical and operator in c

 

In the above example, the printf function is used to print the smallest number among the num1, num2, and num3.

 

Logical OR Operator (||):

The || operator yields 1 if either of its operands is non-zero; otherwise, it yields 0. The logical OR Operator result type is int and both operands must be scalar type.

Its syntax is following,

expression_1 || expression_2

 

✍ The || operator guarantees left-to-right evaluation.

 

Flow Chart of Logical OR Operator:

Following is a flow chart of the OR Operator.

logical OR operator in C

The || operator works as follows:

✍ The first operand (expression_1) must be a scalar type (“single” value like integer, boolean). It is evaluated first; there is a sequence point between its evaluation and the evaluation of the second operand if the second operand is evaluated.

✍ The second operand (expression_2) is evaluated only if the first operand evaluates to azero (0) value. It is known as short-circuit evaluation., for example,

//code

if((a == d) || (a == b) || (a == c))
{
  //some message
}

In the above example if expression (a ==d) evaluates true, then other expressions will skip by the compiler.

✍ Logical OR expression evaluate 0 only if all operand evaluates false.

 

The following table explains the result of the OR operator (||) on two operands.

Operand 1 Operand 2 Result
false true 1
false false 0
true Not-evaluate 1

 

 

Example for Logical OR Operator (||):

The following example illustrates the use of Logical OR Operator:

#include<stdio.h>
int main()
{
    int num = 14, ret;

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

    //logical or expression
    ret = (num > 11) || (num++);

    printf("Result of logical expression = %d\n", ret);

    printf("Final value of num = %d\n", num);

    return 0;
}

Output:

Initial value of num = 14
Result of logical expression = 1
Final value of num = 14

 

In logical OR expression, if the first operand evaluates true, then the second operand is not evaluated. The above example code explains the same concept. The expression num >11 is true, so the right operand num++ is not evaluated.

 

Logical NOT Operator (!):

The ! operator yields 0 if its operand is non-zero; otherwise, it yields 0. The logical NOT Operator result type is int and operand must be scalar type. It is a unary operator, not a binary operator like && and ||.

Its syntax is following,

!expression

The expression !E is equivalent to (0==E).

 

The following table explains the result of the NOT operator (!) on operands.

Operand Result
NON-ZERO 0
Zero 1

 

Example Code,

#include <stdio.h>

int main()
{
    int num = 0;

    // !num will yield 1
    if (!num)
    {
        printf("Logical NOT Expression yields true\n");
    }
    else
    {
        printf("Logical NOT Expression yields false\n");
    }

    //assign num to non-zero value
    num = 90;

    /*
       Since non-zero value is considered to be true.
       So !num will yield false
    */
    if (!num)
    {
        printf("Logical NOT Expression yields true\n");
    }
    else
    {
        printf("Logical NOT Expression yields false\n");
    }

    return 0;
}

Output:

Logical NOT Expression yields true
Logical NOT Expression yields false

 

Recommended Post:

Leave a Reply

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