← Back to C Course | Chapter 3: Operators | Lesson 3 of 9

C Relational Operators

Relational operators compare two values, like asking who is taller. The answer is either true or false.
Syntax
c
result = value1 == value2;  // also != > < >= <=

Equal (==) and Not Equal (!=)

== tests whether two values are equal and != tests whether they're different; both produce 1 for true or 0 for false, and mixing up == with the single = assignment operator is one of the most common C bugs.

Example: Equal (==) and Not Equal (!=)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int a = 5, b = 5;
	// Print formatted text to the screen
	printf("%d %d", a == b, a != b);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Greater Than (>) and Greater Than or Equal (>=)

> and >= test whether the left operand is strictly greater, or greater than or equal to, the right operand -- commonly used to check upper bounds, like whether a score exceeds a passing threshold.

Example: Greater Than (>) and Greater Than or Equal (>=)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare score and set it to 75
	int score = 75;
	// Print formatted text to the screen
	printf("%d %d", score > 70, score >= 75);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Less Than (<) and Less Than or Equal (<=)

< and <= test whether the left operand is strictly less, or less than or equal to, the right operand -- the mirror image of > and >=, often paired with them to check whether a value falls within a range.

Example: Less Than (<) and Less Than or Equal (<=)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare age and set it to 15
	int age = 15;
	// Print formatted text to the screen
	printf("%d %d", age < 18, age <= 15);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Understanding Relational Outputs

C has no dedicated boolean type in its original standard -- relational and logical expressions simply evaluate to the integer 1 for true or 0 for false, which is why you can use their results directly in arithmetic or as an if condition.

Example: Understanding Relational Outputs

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare result and set it to (5 > 3)
	int result = (5 > 3);
	// Print formatted text to the screen
	printf("%d", result);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Conditional Expressions

Relational operators are what make conditional logic possible -- every if statement, while loop condition, and validation check in a C program ultimately relies on one of these operators producing a true/false result.

Example: Conditional Expressions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare age and set it to 20
	int age = 20;
	// Check whether age >= 18
	if (age >= 18) {
		// Print formatted text to the screen
		printf("Adult");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing if (a = b) instead of a == b, which assigns instead of comparing.
  2. Chaining comparisons like 1 < x < 10, which is always true.
  3. Comparing floating-point values with ==, where rounding errors cause surprising results.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.