Open In App

Relational Operators in C

In C, relational operators are the symbols that are used for comparison between two values to understand the type of relationship a pair of numbers shares. The result that we get after the relational operation is a boolean value, that tells whether the comparison is true or false. Relational operators are mainly used in conditional statements and loops to check the conditions in C programming.

Types of C Relational Operators

There are a total of 6 relational operators in C language. There are:

1. Equal to operator (==)

The C equal to operator (==) is a relational operator that is used to check whether the two given operands are equal or not.

Syntax

operand1 == operand2

For example, 5==5 will return true.

2. Not equal to operator (!=)

The C not equal (==) to operator is another relational operator used for checking whether the two given operands are equal or not.

Syntax

operand1 != operand2

For example, 5!=5 will return false.

3. Greater than operator (>)

The greater than operator is a relational operator in C that checks whether the first operand is greater than the second operand or not.

Syntax

operand1 > operand2

For example, 6>5 will return true.

4. Less than operator (<)

The less than operator is a relational operator in C that checks whether the first operand is lesser than the second operand.

Syntax

operand1 < operand2

For example, 6<5 will return false.

Note: The greater than and less than operator are not equal to the complement of each other.

5. Greater than or equal to operator (>=)

The greater than or equal to the operator is a relational operator in C that checks whether the first operand is greater than or equal to the second operand.

Syntax

operand1 >= operand2

For example, 5>=5 will return true.

6. Less than or equal to the the operator (<=)

The less than or equal to the operator is a relational operator in C that checks whether the first operand is less than or equal to the second operand.

Syntax

operand1 <= operand2

For example, 5<=5 will also return true.

Example of Relational Operator in C

The below example demonstrates the use of all relational operators discussed above:




// C program to demonstrate working of relational operators
#include <stdio.h>
  
int main()
{
    int a = 10, b = 4;
  
    // greater than example
    if (a > b)
        printf("a is greater than b\n");
    else
        printf("a is less than or equal to b\n");
  
    // greater than equal to
    if (a >= b)
        printf("a is greater than or equal to b\n");
    else
        printf("a is lesser than b\n");
  
    // less than example
    if (a < b)
        printf("a is less than b\n");
    else
        printf("a is greater than or equal to b\n");
  
    // lesser than equal to
    if (a <= b)
        printf("a is lesser than or equal to b\n");
    else
        printf("a is greater than b\n");
  
    // equal to
    if (a == b)
        printf("a is equal to b\n");
    else
        printf("a and b are not equal\n");
  
    // not equal to
    if (a != b)
        printf("a is not equal to b\n");
    else
        printf("a is equal b\n");
  
    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

Article Tags :