What is relational operator in C language?





Relational operators are symbols used to establish relationships between two values or expressions. These operators return a Boolean value, i.e., either true or false, depending on whether the relationship between the operands holds true or not. In most programming languages, including C, relational operators are primarily used in conditional statements and loops to control the flow of the program.



Here are the common relational operators in C:

Relational Operators In C With Example
Operator Description Example Result
Equal to (==) Checks if two operands are equal. Returns true if they are equal, otherwise false. 5 == 5 true
Not equal to (!=) Checks if two operands are not equal. Returns true if they are not equal, otherwise false. 7 != 3 true
Greater than (>) Checks if the left operand is greater than the right operand. Returns true if it is, otherwise false. 8 > 3 true
Less than (<) Checks if the left operand is less than the right operand. Returns true if it is, otherwise false. 4 < 2 false
Greater than or equal to (>=) Checks if the left operand is greater than or equal to the right operand. Returns true if it is, otherwise false. 6 >= 6 true
Less than or equal to (<=) Checks if the left operand is less than or equal to the right operand. Returns true if it is, otherwise false. 9 <= 3 false


C program to compare two numbers a and b. If they are equal, print a is equal to b otherwise, print a is not equal to b.

#include <stdio.h>

int main() {
    int a = 5, b = 5;
    if (a == b) {
        printf("%d is equal to %d\n", a, b);
    } else {
        printf("%d is not equal to %d\n", a, b);
    }
    return 0;
}
Output:
5 is equal to 5


C program to check if two numbers a and b are not equal. If they are not equal, print a is not equal to b otherwise, print a is equal to b.

#include <stdio.h>

int main() {
    int a = 7, b = 3;
    if (a != b) {
        printf("%d is not equal to %d\n", a, b);
    } else {
        printf("%d is equal to %d\n", a, b);
    }
    return 0;
}

Output:
7 is not equal to 3


C program to determine if a number a is greater than another number b. If a is greater than b, print a is greater than b otherwise, print a is not greater than b.

#include <stdio.h>

int main() {
    int a = 8, b = 3;
    if (a > b) {
        printf("%d is greater than %d\n", a, b);
    } else {
        printf("%d is not greater than %d\n", a, b);
    }
    return 0;
}
Output:
8 is greater than 3


C program to determine if a number a is less than another number b. If a is less than b, print a is less than b otherwise, print a is not less than b.

#include <stdio.h>

int main() {
    int a = 4, b = 2;
    if (a < b) {
        printf("%d is less than %d\n", a, b);
    } else {
        printf("%d is not less than %d\n", a, b);
    }
    return 0;
}
Output:
4 is not less than 2


C program to check if a number a is greater than or equal to another number b. If a is greater than or equal to b, print a is greater than or equal to b otherwise, print a is neither greater than nor equal to b.

#include <stdio.h>

int main() {
    int a = 6, b = 6;
    if (a >= b) {
        printf("%d is greater than or equal to %d\n", a, b);
    } else {
        printf("%d is neither greater than nor equal to %d\n", a, b);
    }
    return 0;
}
Output:
6 is greater than or equal to 6


C program to determine if a number a is less than or equal to another number b. If a is less than or equal to b, print a is less than or equal to b otherwise, print a is neither less than nor equal to b

#include <stdio.h>

int main() {
    int a = 9, b = 3;
    if (a <= b) {
        printf("%d is less than or equal to %d\n", a, b);
    } else {
        printf("%d is neither less than nor equal to %d\n", a, b);
    }
    return 0;
}
Output:
9 is neither less than nor equal to 3