Relational Operators in C

By | February 18, 2023

Relational operators in C are used for comparing values and determining whether they are equal, not equal, greater than, less than, greater than or equal to, or less than or equal to each other. Here is a list of relational operators in C:

1. Equal to (==): Returns true if the two operands are equal, false otherwise.

int a = 10;
int b = 20;
if (a == b) {
   printf("a is equal to b\n");
}
else {
   printf("a is not equal to b\n");
}

In this example, since a is not equal to b, the output will be “a is not equal to b”.

2. Not equal to (!=): Returns true if the two operands are not equal, false otherwise.

int a = 10;
int b = 20;
if (a != b) {
   printf("a is not equal to b\n");
}
else {
   printf("a is equal to b\n");
}

In this example, since a is not equal to b, the output will be “a is not equal to b”.

3. Greater than (>): Returns true if the first operand is greater than the second operand, false otherwise.

int a = 10;
int b = 20;
if (a > b) {
   printf("a is greater than b\n");
}
else {
   printf("a is not greater than b\n");
}

In this example, since a is not greater than b, the output will be “a is not greater than b”.

4. Less than (<): Returns true if the first operand is less than the second operand, false otherwise.

int a = 10;
int b = 20;
if (a < b) {
   printf("a is less than b\n");
}
else {
   printf("a is not less than b\n");
}

In this example, since a is less than b, the output will be “a is less than b”.

5. Greater than or equal to (>=): Returns true if the first operand is greater than or equal to the second operand, false otherwise.

int a = 10;
int b = 20;
if (a >= b) {
   printf("a is greater than or equal to b\n");
}
else {
   printf("a is less than b\n");
}

In this example, since a is less than b, the output will be “a is less than b”.

6. Less than or equal to (&le): Returns true if the first operand is less than or equal to the second operand, false otherwise.

int a = 10;
int b = 20;
if (a <= b) {
   printf("a is less than or equal to b\n");
}
else {
   printf("a is greater than b\n");
}

In this example, since a is less than b, the output will be “a is less than or equal to b”.

Relational operators return a value of 1 (true) or 0 (false) depending on the result of the comparison, and are often used in control statements such as if-else and while loops to control program flow based on the results of the comparisons.

Example:
Here’s an example code that demonstrates the use of relational operators in C:

#include <stdio.h>

int main() {
    int a = 10;
    int b = 20;

    // Equal to (==)
    if (a == b) {
        printf("a is equal to b\n");
    } else {
        printf("a is not equal to b\n");
    }

    // Not equal to (!=)
    if (a != b) {
        printf("a is not equal to b\n");
    } else {
        printf("a is equal to b\n");
    }

    // Greater than (>)
    if (a > b) {
        printf("a is greater than b\n");
    } else {
        printf("a is not greater than b\n");
    }

    // Less than (<)
    if (a < b) {
        printf("a is less than b\n");
    } else {
        printf("a is not less than b\n");
    }

    // Greater than or equal to (>=)
    if (a >= b) {
        printf("a is greater than or equal to b\n");
    } else {
        printf("a is less than b\n");
    }

    // Less than or equal to (<=)
    if (a <= b) {
        printf("a is less than or equal to b\n");
    } else {
        printf("a is greater than b\n");
    }

    return 0;
}

When you run this code, you will see the following output:

a is not equal to b
a is not equal to b
a is not greater than b
a is less than b
a is less than b
a is less than or equal to b

This output shows the results of comparing the values of a and b using each of the six relational operators. In this case, the values of a and b are such that only the last two comparisons return true, since a is less than b.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.