C Relational Operators – Compare Values in C

Relational operators in C are used to compare two values or variables. The result of a comparison is either true (1) or false (0), which is useful in decision-making (e.g., in if, while, or for statements).

What Are Relational Operators?

Relational operators help check the relationship between two expressions or variables. They return a Boolean result:

 

ConditionResult
Condition is true1
Condition is false0

List of Relational Operators in C

OperatorNameExampleMeaning
==Equal toa == bTrue if a equals b
!=Not equal toa != bTrue if a is not b
>Greater thana > bTrue if a is greater than b
<Less thana < bTrue if a is less than b
>=Greater than or equal toa >= bTrue if a is ≥ b
<=Less than or equal toa <= bTrue if a is ≤ b

Example Program Using Relational Operators

#include <stdio.h>

int main() {
    int a = 10, b = 5;

    printf("a == b: %d\n", a == b);   // 0 (false)
    printf("a != b: %d\n", a != b);   // 1 (true)
    printf("a > b: %d\n", a > b);     // 1 (true)
    printf("a < b: %d\n", a < b);     // 0 (false)
    printf("a >= b: %d\n", a >= b);   // 1 (true)
    printf("a <= b: %d\n", a <= b);   // 0 (false)

    return 0;
}

 Output:

a == b: 0
a != b: 1
a > b: 1
a < b: 0
a >= b: 1
a <= b: 0

Use Cases

  • if (a > b) – Decision-making

  • while (x <= 100) – Loops

  • for (i = 0; i != n; i++) – Iteration with condition

Best Practices

PracticeWhy?
Always use == to compare valuesAvoid accidental assignment with =
Use parentheses for clarityImproves readability in conditions
Use relational operators in if or while loopsCommon in control flow

Common Mistakes to Avoid

MistakeWhy it’s Wrong
Using = instead of === assigns, doesn’t compare
Not understanding return valuestrue = 1, false = 0
Ignoring data typesComparing int to char without cast may confuse
Scroll to Top