Relational Operators in C

In this tutorials, you will learn about the Relational Operations in C and their usage and examples. Relational Operations are mainly used to compare values, such as in the ‘if’ statements, which will be discussed later in this course.

Relational Operations in C with Example

The table below illustrates the Relational Operators in C, a brief description, and a simple example.

OperatorDescriptionExample
To checks if the values of two operands are equal or not.x == y
!=To checks if the values of two operands are equal or not.x != y
>To checks if the value of the left operand is greater than the value of the right operand. x>y
<To checks if the value of the left operand is less than the value of the right operand.x
>=To checks if the value of the left operand is greater than or equal to the value of the right operand. x>=y
<=To checks if the value of the left operand is less than or equal to the value of the right operand. x<=y

Example of Relational Operators in C

#include <stdio.h>

int main()
{
    int x = 5, y = 10;


    // == example
    if (x == y)
        printf("x is equal to y\n");
    else
        printf("x is not equal to y\n");

    // != example
    if (x != y)
        printf("x is different than y\n");
    else
        printf("x is not different than y\n");

    // > example
    if (x > y)
        printf("x is greater than y\n");
    else
        printf("x is not greater than y\n");

    // < example
    if (x < y)
        printf("x is less than y\n");
    else
        printf("x is not less than y\n");

    // >= example
    if (x >= y)
        printf("x is greater than or equal to y\n");
    else
        printf("x is not greater than or equal to y\n");

    // <= example
    if (x <= y)
        printf("x is less than or equal to y\n");
    else
        printf("x is not less than or equal to y\n");

    return 0;
}

Result

x is not equal to y
x is different than y
x is not greater than y
x is less than y
x is not greater than or equal to y
x is less than or equal to y
Back to: Learn C Programming > Expression and Operators in C

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.