Relational Operators in C

Relational operators are used to compare two variables.

It will return 1 , if the comparison or relation is true.

otherwise it will return 0.




Example

Operator

Description

Example   (true=1,false=0)

==

Checks both left and right operands are same

10 == 10 it will return true.
10 == 7 it will return false

!=

Checks both left and right operands are not same.

10 != 20 it will return true.
10 != 10 it will return false.

<

Checks whether the left operand is smaller than the right operand.

5 < 10 it will return true.
10 < 10 it will return false.

<=

Checks whether the left operand is smaller than or equal to the right operand.

10 <= 10 it will return true
20 <= 10 it will return false.

>

Checks whether the left operand is greater than the right operand.

50 > 10 it will return true
5 > 10 it will return false.

>=

Checks whether the left operand is greater than or equal to the right operand.

10 >= 10 it will return true
5 >= 10 it will return false.





Sample Program

Example

#include<stdio.h>

int main()
{
    int a = 5, b = 10;

    printf("Return value of (%d == %d) is %d\n", a, b, a == b); //return 0
    printf("Return value of (%d != %d) is %d\n", a, b, a != b); //return 1
    printf("Return value of (%d <  %d) is %d\n", a, b, a < b);  //return 1
    printf("Return value of (%d <= %d) is %d\n", a, b, a <= b); //return 1
    printf("Return value of (%d >  %d) is %d\n", a, b, a > b);  //return 0
    printf("Return value of (%d >= %d) is %d\n", a, b, a >= b); //return 0

    return 0;
}

Relational operators are frequently used in decision making and looping statements. We will discuss about it in the future topics.


Topics You Might Like