Relational operators: <, <=, >, and >=
What you'll learn: How to use mathematical symbols to compare two values and determine if a relationship between them is true or false.
In the previous lesson, we looked at how C treats truth. Now, we need a way to generate those "true" or "false" results using data. This is where relational operators come in. They act like a scale, weighing two values against each other to see how they relate in size.
Thinking like a gatekeeper
Imagine you are a bouncer at a club. Your only job is to check a person’s age against a rule. If the rule is "you must be 21 or older," you are performing a relational comparison. In C, these operators ask a simple question: "Is this statement true?" If the answer is yes, the expression evaluates to 1 (true). If not, it evaluates to 0 (false).
Here are the four basic "inequality" operators:
<(Less than): Is the left side smaller than the right?<=(Less than or equal to): Is the left side smaller than or at least the same as the right?>(Greater than): Is the left side bigger than the right?>=(Greater than or equal to): Is the left side bigger than or at least the same as the right?
Seeing it in code
When you use these in a program, they are usually placed between two variables or numbers. While we often use them inside if statements (which we will cover soon), they work perfectly fine on their own as math expressions.
#include <stdio.h>
int main() {
int myAge = 25;
int votingAge = 18;
int maxCapacity = 25;
// Is 25 greater than 18? (Yes, so result is 1)
int canVote = myAge > votingAge;
// Is 25 less than 18? (No, so result is 0)
int isMinor = myAge < votingAge;
// Is 25 greater than or equal to 25? (Yes, it's equal!)
int isFull = myAge >= maxCapacity;
printf("Can vote: %d\n", canVote);
printf("Is minor: %d\n", isMinor);
printf("Is full: %d\n", isFull);
return 0;
}
The "Equal To" distinction
A common point of confusion is the difference between > and >=. If your limit is 10 and your value is 10:
value > 10is false (10 is not strictly bigger than 10).value >= 10is true (10 is equal to 10).
Always choose the operator that matches your specific boundary logic. If a player needs more than 100 points to win, use >. If they win the moment they hit 100, use >=.
Key takeaway
Relational operators compare two values and resolve to 1 (true) or 0 (false) based on their relative size.