Back to C Programming
2026-07-125 min read

Relational Operators (C Programming)

Learn Relational Operators (C Programming) step by step with clear examples and exercises.

Why This Matters

Understanding relational operators is essential for writing efficient and effective C programs. These operators enable you to compare values, making it easier to create conditions for decision-making and looping constructs. Mastery of relational operators is crucial in solving real-world programming problems and debugging common issues. In interviews, you'll often encounter questions about relational operators as they play a significant role in the core logic of many C programs.

Prerequisites

Before diving into relational operators, you should have a good understanding of C data types, variables, and basic syntax. Familiarity with control structures like if statements and loops (for, while, do-while) is also important. Additionally, having a solid grasp of C's type promotion rules will help you avoid common pitfalls when comparing operands of different data types.

Core Concept

In C programming, relational operators are used to compare two operands and return a boolean value (true or false). The comparison results in either the operands being equal, greater than, less than, greater than or equal to, or less than or equal to. Here's a list of all relational operators:

  • == (equal to)
  • != (not equal to)
  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)

Comparing Variables

Let's compare two variables, a and b, using relational operators:

int a = 10;
int b = 20;

if (a == b) {
printf("a is equal to b.\n");
} else if (a != b) {
printf("a is not equal to b.\n");
} else if (a > b) {
printf("a is greater than b.\n");
} else if (a < b) {
printf("a is less than b.\n");
} else if (a >= b) {
printf("a is greater than or equal to b.\n");
} else if (a <= b) {
printf("a is less than or equal to b.\n");
}

In this example, the output will be "a is less than b." because a is less than b.

Comparing Operands with Different Data Types

Note that that comparing operands of different data types can lead to unexpected results due to type promotion rules in C. For example:

char c = 'A';
int i = 10;

if (c > i) { // This will not be printed
printf("c is greater than i.\n");
}

In this case, c is promoted to an int, and since the ASCII value of 'A' is less than 10, the condition will evaluate to false. To avoid such issues, you should ensure that operands have compatible data types or explicitly cast one operand if necessary.

Worked Example

Let's write a simple program that checks if a number is even or odd:

#include <stdio.h>

int main() {
int num;

printf("Enter an integer: ");
scanf("%d", &num);

if (num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}

return 0;
}

In this example, the program asks for user input and checks if the remainder of the division by 2 is equal to 0 (i.e., the number is even). If not, it prints that the number is odd.

Common Mistakes

Forgetting Semicolon After Control Structures

One common mistake is forgetting to include a semicolon after control structures like if, for, and while. This can lead to syntax errors:

if (a > b)
printf("a is greater than b.\n"); // Syntax error!

Comparing Floating-Point Numbers with Equal Operator

When comparing floating-point numbers, using the == operator can lead to unexpected results due to rounding errors. Instead, you should use an epsilon value (a small positive number) to compare them more accurately:

float a = 0.1f;
float b = 0.2f;
float EPSILON = 0.00001f;

if (fabs(a - b) < EPSILON) {
printf("a is approximately equal to b.\n");
}

Comparing Operands of Different Data Types

Comparing operands of different data types can lead to unexpected results due to type promotion rules in C. For example, comparing a character and an integer will promote the character to an integer, which might not give the desired result:

char c = 'A';
int i = 10;

if (c > i) { // This will not be printed
printf("c is greater than i.\n");
}

To avoid such issues, you should ensure that operands have compatible data types or explicitly cast one operand if necessary:

char c = 'A';
int i = 10;

if ((int)c > i) { // Now the comparison is valid
printf("c is greater than i.\n");
}

Practice Questions

  1. Write a program that checks if a number is prime or composite.
  2. Write a program that finds the largest of three numbers using relational operators.
  3. Given two strings, write a program that compares them lexicographically (i.e., character by character).
  4. Write a program that calculates the average of three floating-point numbers and checks if it's greater than 50 using relational operators.
  5. Write a program that sorts an array of integers in ascending order using only relational operators and a for loop.

FAQ

Why can't I use the == operator for floating-point comparison?

Using the == operator for floating-point numbers can lead to unexpected results due to rounding errors. Instead, you should use an epsilon value (a small positive number) to compare them more accurately.

Can I compare strings using relational operators in C?

No, you cannot directly compare strings using relational operators in C. You should use string comparison functions like strcmp() instead.

What happens when I compare operands of different data types with relational operators?

Comparing operands of different data types can lead to unexpected results due to type promotion rules in C. For example, comparing a character and an integer will promote the character to an integer, which might not give the desired result. To avoid such issues, you should ensure that operands have compatible data types or explicitly cast one operand if necessary.