Tutorialspoint.dev

Operators in C | Set 2 (Relational and Logical Operators)

We have discussed introduction to operators in C and Arithmetic Operators. In this article, Relational and Logical Operators are discussed.

Relational Operators:
Relational operators are used for comparison of two values. Let’s see them one by one:



  • ‘==’ operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false. For example, 5==5 will return true.
  • ‘!=’ operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise it returns false. It is the exact boolean complement of the ‘==’ operator. For example, 5!=5 will return false.
  • ‘>’ operator checks whether the first operand is greater than the second operand. If so, it returns true. Otherwise it returns false. For example, 6>5 will return true.
  • ‘<‘ operator checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise it returns false. For example, 6<5 will return false.
  • ‘>=’ operator checks whether the first operand is greater than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example, 5>=5 will return true.
  • ‘<=’ operator checks whether the first operand is lesser than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example, 5<=5 will also return true.
// C program to demonstrate working of relational operators
#include <stdio.h>
  
int main()
{
    int a=10, b=4;
  
    // relational operators
    // greater than example
    if (a > b)
        printf("a is greater than b ");
    else printf("a is less than or equal to b ");
  
    // greater than equal to
    if (a >= b)
        printf("a is greater than or equal to b ");
    else printf("a is lesser than b ");
  
    // less than example
    if (a < b)
        printf("a is less than b ");
    else printf("a is greater than or equal to b ");
  
    // lesser than equal to
    if (a <= b)
        printf("a is lesser than or equal to b ");
    else printf("a is greater than b ");
  
    // equal to
    if (a == b)
        printf("a is equal to b ");
    else printf("a and b are not equal ");
  
    // not equal to
    if (a != b)
        printf("a is not equal to b ");
    else printf("a is equal b ");
  
    return 0;
}

Output:

a is greater than b
a is greater than or equal to b
a is greater than or equal to b
a is greater than b
a and b are not equal
a is not equal to b



Logical Operators:
They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. They are described below:

  • Logical AND: The ‘&&’ operator returns true when both the conditions in consideration are satisfied. Otherwise it returns false. For example, a && b returns true when both a and b are true (i.e. non-zero).
  • Logical OR: The ‘||’ operator returns true when one (or both) of the conditions in consideration is satisfied. Otherwise it returns false. For example, a || b returns true if one of a or b is true (i.e. non-zero). Of course, it returns true when both a and b are true.
  • Logical NOT: The ‘!’ operator returns true the condition in consideration is not satisfied. Otherwise it returns false. For example, !a returns true if a is false, i.e. when a=0.
// C program to demonstrate working of logical operators
#include <stdio.h>
  
int main()
{
    int a=10, b=4, c = 10, d = 20;
  
    // logical operators
  
    // logical AND example
    if (a>b && c==d)
        printf("a is greater than b AND c is equal to d ");
    else printf("AND condition not satisfied ");
  
    // logical AND example
    if (a>b || c==d)
        printf("a is greater than b OR c is equal to d ");
    else printf("Neither a is greater than b nor c is equal "
                " to d ");
  
    // logical NOT example
    if (!a)
        printf("a is zero ");
    else printf("a is not zero");
  
    return 0;
}

Output:

AND condition not satisfied
a is greater than b OR c is equal to d
a is not zero

Short-Circuiting in Logical Operators:
In case of logical AND, the second operand is not evaluated if first operand is false. For example, program 1 below doesn’t print “GeeksQuiz” as the first operand of logical AND itself is false.



#include <stdio.h>
#include <stdbool.h>
int main()
{
    int a=10, b=4;
    bool res = ((a == b) && printf("GeeksQuiz"));
    return 0;
}

But below program prints “GeeksQuiz” as first operand of logical AND is true.

#include <stdio.h>
#include <stdbool.h>
int main()
{
    int a=10, b=4;
    bool res = ((a != b) && printf("GeeksQuiz"));
    return 0;
}

 
In case of logical OR, the second operand is not evaluated if first operand is true. For example, program 1 below doesn’t print “GeeksQuiz” as the first operand of logical OR itself is true.

#include <stdio.h>
#include <stdbool.h>
int main()
{
    int a=10, b=4;
    bool res = ((a != b) || printf("GeeksQuiz"));
    return 0;
}

But below program prints “GeeksQuiz” as first operand of logical OR is false.

#include <stdio.h>
#include <stdbool.h>
int main()
{
    int a=10, b=4;
    bool res = ((a == b) || printf("GeeksQuiz"));
    return 0;
}

Quiz on Operators in C



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter