Skip to content
StudyFame

Relational Operators in C

Programs make decisions all the time: Is the age 18 or more? Are the marks above 40? Is the password correct? To answer these, you compare values using relational operators.

In this lesson you will learn the six relational operators, what they return, and the most common bug in C: writing = when you mean ==.

What You Will Learn

  • The 6 relational (comparison) operators.
  • Why they return 1 or 0.
  • The difference between = and ==.
  • Why comparing decimal numbers with == is risky.
  • How to compare characters.

Relational Operators in C Explained

Exam definition: Relational operators compare two values. The result is 1 if the comparison is true and 0 if it is false. C has six relational operators: ==, !=, >, <, >=, <=.

OperatorMeaningExample (a = 10, b = 20)Result
==Equal toa == b0
!=Not equal toa != b1
>Greater thana > b0
<Less thana < b1
>=Greater than or equal toa >= 101
<=Less than or equal tob <= 150

Difference between = and ==

= (assignment)== (equality)
Stores a valueCompares two values
x = 5 puts 5 into xx == 5 asks "is x equal to 5?"
Result is the stored value (5)Result is 1 (true) or 0 (false)
Assignment operatorRelational operator

Memory trick: One = gives (puts a value in). Two == ask (are they the same?). Read x == 5 aloud as "x is equal to 5".

Syntax of Relational Operators

value1 operator value2      /* result: 1 (true) or 0 (false) */

if (age >= 18) { ... }       /* used mostly in conditions */
int is_pass = marks >= 40;   /* or stored as 1 or 0 */

Example 1: All Relational Operators

#include <stdio.h>

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

    printf("a == b : %d\n", a == b);
    printf("a != b : %d\n", a != b);
    printf("a >  b : %d\n", a > b);
    printf("a <  b : %d\n", a < b);
    printf("a >= 10: %d\n", a >= 10);
    printf("b <= 15: %d\n", b <= 15);
    printf("'A' < 'B': %d\n", 'A' < 'B');

    return 0;
}

Output

a == b : 0
a != b : 1
a >  b : 0
a <  b : 1
a >= 10: 1
b <= 15: 0
'A' < 'B': 1

Code Explanation

  1. Each comparison gives 1 (true) or 0 (false), and printf prints that number.
  2. a >= 10: 10 is equal to 10, so "greater than or equal" is true.
  3. 'A' < 'B': characters are compared by their ASCII codes: 65 < 66, so true.

How Comparisons Work

  • C has no separate true/false type in classic C. It uses numbers: 0 means false, and any non-zero value means true.
  • Relational operators always produce exactly 1 or 0.
  • In if (x = 5), the assignment gives 5, which is non-zero, so the condition is always true. That is why the = vs == mistake is so dangerous: no error, just wrong behaviour.
  • Decimal numbers are stored approximately. 0.1 + 0.2 == 0.3 is false in C. For decimals, check if the difference is very small instead.

Example 2: The = vs == Bug

#include <stdio.h>

int main(void)
{
    int marks = 45;

    if (marks = 100)          /* BUG: assigns 100, always true */
        printf("Full marks!\n");

    printf("marks is now %d\n", marks);

    if (marks == 45)          /* correct comparison */
        printf("marks is 45\n");
    else
        printf("marks is not 45\n");

    return 0;
}

Output

Full marks!
marks is now 100
marks is not 45

The student had 45, but the program printed "Full marks!" and changed the marks to 100. Compiling with -Wall warns you about this: suggest parentheses around assignment used as truth value.

Important Points to Remember

  • 6 relational operators: == != > < >= <=.
  • They return 1 (true) or 0 (false).
  • = assigns, == compares.
  • In C, 0 is false and any non-zero value is true.
  • Don't compare floats with ==.

Common Mistakes Beginners Make

MistakeWhat happensFix
if (x = 0)Assigns 0; the condition is always falseif (x == 0)
=> or =<Syntax errorWrite >= and <= (the = comes second)
if (10 < x < 20)Always true: (10 < x) is 0 or 1, and both are < 20if (x > 10 && x < 20)
if (a == 0.3) with floatsMay be false even when it looks equalCheck fabs(a - 0.3) < 0.0001
Comparing strings with ==Compares addresses, not textUse strcmp()

Relational Operators Practice Questions

  1. Name the six relational operators in C.
  2. What do relational operators return?
  3. Write two differences between = and ==.
  4. What is the output of printf("%d", 5 > 3 > 1);?
  5. Why is if (x = 5) always true?
Show answers
  1. ==, !=, >, <, >=, <=.
  2. 1 if the comparison is true, 0 if it is false.
  3. = stores a value and returns it; == compares two values and returns 1 or 0.
  4. 0. 5 > 3 is 1, then 1 > 1 is 0.
  5. It assigns 5 to x, and the expression's value is 5, which is non-zero, so it is true.

Coding Exercise

Read a person's age and print 1 if they can vote (18 or more) and 0 if not, using a relational operator only (no if).

Show solution
#include <stdio.h>

int main(void)
{
    int age;

    printf("Enter age: ");
    scanf("%d", &age);

    printf("Can vote: %d\n", age >= 18);
    return 0;
}
Enter age: 17
Can vote: 0

Quick Quiz: Relational Operators in C (MCQ)

  1. What does a true comparison return in C?

    A) true   B) 1   C) −1   D) yes

  2. Which checks if x is NOT equal to 5?

    A) x =! 5   B) x <> 5   C) x != 5   D) x !== 5

  3. Output of printf("%d", 7 <= 7);

    A) 0   B) 1   C) 7   D) Error

  4. = is a ______ operator.

    A) Relational   B) Logical   C) Assignment   D) Bitwise

  5. Output of printf("%d", 'a' > 'A');

    A) 0   B) 1   C) 32   D) Error

Show answers
  1. B: 1.
  2. C: x != 5
  3. B: 1.
  4. C: assignment.
  5. B: 1 ('a' is 97, 'A' is 65).

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Operators== != > < >= <=
Result1 (true) or 0 (false)
=Assignment: stores
==Equality: compares
True in CAny non-zero value
5 > 3 > 10
Compare stringsstrcmp(), not ==

Frequently asked questions

What is the difference between = and == in C?
= assigns a value; == compares two values and returns 1 or 0.
What do relational operators return?
1 for true, 0 for false (an int).
Why is if (x = 5) a bug?
It assigns 5 instead of comparing, and 5 is non-zero, so the condition is always true.
Why does 10 < x < 20 not work in C?
It becomes (10 < x) < 20, i.e. 0 or 1 < 20, which is always true. Use x > 10 && x < 20.

Remember

  • One = gives, two == ask.
  • 0 = false, non-zero = true.

Next Topic

Next lesson: Logical Operators in C. You will learn to join conditions with AND, OR and NOT.