Relational Operators in C

In this tutorial, we will understand relational operators and their uses in the C programming language.


What is a Relational Operator?

A relational operator is a symbol that compares two values. It tells us if the values are the same or if one is greater or smaller than the other. The result of the comparison is either true or false. We can represent true as 1 and false as 0. The most commonly used relational operators are:

  1. Equal to (=)
  2. Not equal to (<>)
  3. Greater than (>)
  4. Greater than or equal to (>=)
  5. Less than (<)
  6. Less than or equal to (<=)

Relational Operators in Details

  • Equal to (=): It checks if two values are the same. If they are, it returns true; otherwise, it returns false. For example, 6 == 6 is true, but 8 == 9 is false.
  • Not equal to (!=): It checks if two values are different. If they are not the same, it returns true; otherwise, it returns false. For example, 6 != 6 is false, but 8 != 9 is true.
  • Greater than (>): It returns true if the first value is greater than the second value; otherwise, it returns false. For example, 5 > 4 is true, but 4 > 5 is false.
  • Greater than or equal to (>=): It returns true if the first value is greater than or equal to the second value; otherwise, it returns false. For example, 5 >= 5 is true, but 4 >= 5 is false.
  • Less than (<): It returns true if the first value is less than the second value; otherwise, it returns false. For example, 4 < 5 is true, but 5 < 4 is false.
  • Less than or equal to (<=): It returns true if the first value is less than or equal to the second value; otherwise, it returns false. For example, 4 <= 4 is true, but 5 <= 4 is false

Example Program

The following program shows the results generated by all the relational operators:

#include <stdio.h>

int main()
{
    int x = 5;
    int y = 4;

    printf("%d\n", x == y);  // 0
    printf("%d\n", x != y);  // 1
    printf("%d\n", x > y);   // 1
    printf("%d\n", x >= y);  // 1
    printf("%d\n", x < y);   // 0
    printf("%d\n", x <= y);  // 0

    return 0;
}

Output:

0
1
1
1
0
0

The usage of these relational operators will become clear when we discuss conditionals like if-else statements.


Precedence and Associativity of Relational Operators

The following table shows the precedence and associativity of relational operators (in continuation with the arithmetic operators):

PrecedenceOperatorsAssociativity
1stUnary + and –Right to left
2nd*, /, %Left to right
3rd+, – Left to right
4th<, <=, >, >=Left to right
5th==, !=Left to right
6th=Right to left

Example:  Solve 3 + 4 * 9 <= 10 == 5.
Solution:

3 + 4 * 9 <= 10 == 5 
3 + 36 <= 10 == 5           (4 * 9 = 36)
39 <= 10 == 5               (3 + 36 = 39)
0 == 5                      (39 <= 10 = 0)
0


Leave a comment

Leave a comment

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

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.