Relational Operators in C: Comparing Values

Relational operators in C enable the comparison of two values, yielding a result of True or False. Although C lacks a Boolean data type, it interprets "0" as False and any non-zero value as True, making these operators essential for decision-making in programs.



Relational Operators in C

Relational operators in C are used to compare two values and evaluate to either True or False. Since C doesn’t have a Boolean data type, "0" is interpreted as False and any non-zero value is treated as True.

Relational Operators

The following table lists all the relational operators in C:

Operator Description Example
== Checks if the values of two operands are equal. (A == B)
!= Checks if the values of two operands are not equal. (A != B)
> Checks if the left operand is greater than the right. (A > B)
< Checks if the left operand is less than the right. (A < B)
>= Checks if the left operand is greater than or equal to the right. (A >= B)
<= Checks if the left operand is less than or equal to the right. (A <= B)

Example 1

Here is a simple example of relational operators in C:

Code

#include <stdio.h>

int main() {
    int op1 = 5;
    int op2 = 3;
    
    printf("op1: %d op2: %d op1 < op2: %d\n", op1, op2, op1 < op2);
    
    return 0;
}
Output

op1: 5 op2: 3 op1 < op2: 0

Detailed Examples

Example 2

The following example shows all the relational operators in use:

Code

#include <stdio.h>

int main() {
    int a = 21;
    int b = 10;
    
    printf("a: %d b: %d\n", a, b);
    
    if (a == b) {
        printf("Line 1 - a is equal to b\n");
    } else {
        printf("Line 1 - a is not equal to b\n");
    }
    
    if (a < b) {
        printf("Line 2 - a is less than b\n");
    } else {
        printf("Line 2 - a is not less than b\n");
    }
    
    if (a > b) {
        printf("Line 3 - a is greater than b\n");
    } else {
        printf("Line 3 - a is not greater than b\n");
    }
    
    a = 5;
    b = 20;
    
    printf("a: %d b: %d\n", a, b);
    
    if (a <= b) {
        printf("Line 4 - a is either less than or equal to b\n");
    }
    
    if (b >= a) {
        printf("Line 5 - b is either greater than or equal to a\n");
    }
    
    if (a != b) {
        printf("Line 6 - a is not equal to b\n");
    } else {
        printf("Line 6 - a is equal to b\n");
    }
    
    return 0;
}
Output

a: 21 b: 10
Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b

a: 5 b: 20
Line 4 - a is either less than or equal to b
Line 5 - b is either greater than or equal to a
Line 6 - a is not equal to b

Example 3

The == operator should be used with care. Here’s what happens if = is used instead of ==:

Code

#include <stdio.h>

int main() {
    int a = 5;
    int b = 3;
    
    if (a = b) {
        printf("a is equal to b");
    } else {
        printf("a is not equal to b");
    }
    
    return 0;
}
Output

a is equal to b

Example 4

Relational operators can also be used with char types, as char is a subset of int:

Code

#include <stdio.h>

int main() {
    char a = 'B';
    char b = 'd';
    
    printf("a: %c b: %c\n", a, b);
    
    if (a == b) {
        printf("Line 1 - a is equal to b\n");
    } else {
        printf("Line 1 - a is not equal to b\n");
    }
    
    if (a < b) {
        printf("Line 2 - a is less than b\n");
    } else {
        printf("Line 2 - a is not less than b\n");
    }
    
    if (a > b) {
        printf("Line 3 - a is greater than b\n");
    } else {
        printf("Line 3 - a is not greater than b\n");
    }
    
    if (a != b) {
        printf("Line 4 - a is not equal to b\n");
    } else {
        printf("Line 4 - a is equal to b\n");
    }
    
    return 0;
}
Output

a: B b: d
Line 1 - a is not equal to b
Line 2 - a is less than b
Line 3 - a is not greater than b
Line 4 - a is not equal to b

Relational operators cannot be used for comparing secondary types such as arrays or derived types such as structs or unions.