Relational Operators in C

Last Updated :

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.

Comment
Next Article
Mithlesh Upadhyay Published 18 Feb, 2023 · 4 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Article Tags :

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read