Course contentsShow
← C
Lesson 159 of 1,2193. Operators and ExpressionsFree lesson

Greater than `>` and less than `<`

Greater than > and less than <

What you'll learn: How to use relational operators to compare two numbers and determine their relationship.

In C programming, the computer often needs to make decisions. To do that, it acts like a judge at a weigh-in, comparing two values to see which one is heavier or lighter. This is where the Greater than (>) and Less than (<) operators come in.

How They Work

Think of these operators as a hungry alligator. The alligator’s mouth always opens toward the "tastier," larger meal.

  • a > b: Is a greater than b?
  • a < b: Is a less than b?

When you use these in C, the expression doesn't just sit there—it evaluates to a result. However, unlike math class where the answer might be "Yes" or "No," C uses integers to represent truth. If the comparison is true, the result is 1. If it is false, the result is 0.

Seeing it in Action

Imagine you are writing software for a high-speed elevator. The elevator has a weight limit. You need to check if the current load is safe.

#include <stdio.h>

int main() {
    int weightLimit = 1000;
    int currentLoad = 1200;
    int tooHeavy;

    // Is 1200 greater than 1000? 
    // Yes, so tooHeavy will be 1.
    tooHeavy = currentLoad > weightLimit;

    printf("Is it too heavy? %d\n", tooHeavy);

    // Is 1200 less than 1000? 
    // No, so this will result in 0.
    int isLight = currentLoad < weightLimit;

    printf("Is it light? %d\n", isLight);

    return 0;
}

Why it Matters

These operators are the foundation of logic. You’ll use them to check if a player's health has dropped below zero, if a user has entered a valid age, or if a timer has exceeded its limit. While they look like simple math symbols, they are actually the "questions" that allow your program to branch out and behave differently based on the data it receives.

Remember: these operators compare the values inside the variables. They don't change the variables; they just report back on how they stack up against each other at that exact moment.

Key takeaway

Relational operators compare two values and return 1 for true or 0 for false.