Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Comparison Operators

The symbols < and > that you've used in conditions are comparison operators: they compare two values, and the result is either true or false. C has six of them:

  • == equal to: 5 == 5
  • != not equal to: 5 != 3
  • > greater than: 5 > 3
  • < less than: 3 < 5
  • >= greater than or equal to: 5 >= 5
  • <= less than or equal to: 3 <= 5

All the examples in the list are true. In !=, >= and <=, the equals sign always comes second, with no space in between. Watch the difference between > and >= at the boundary: 18 > 18 is false, but 18 >= 18 is true. “18 or older” is therefore age >= 18.

“Equal” is written with two equals signs, ==, because a single one already has a job: points = 10 assigns, which means it stores 10 in the variable points. points == 10 compares: it asks whether points holds 10, and changes nothing.

Writing = instead of == in a condition is one of the most common mistakes. On this site, the compiler catches it:

#include <stdio.h>

int main(void) {
    int points = 7;

    if (points = 10) {
        printf("Full marks!\n");
    }
    printf("Points: %d\n", points);
    return 0;
}

The compiler reports:

main.c:6:16: error: using the result of an assignment as a condition without parentheses [-Werror,-Wparentheses]
    6 |     if (points = 10) {
      |         ~~~~~~~^~~~
main.c:6:16: note: place parentheses around the assignment to silence this warning
    6 |     if (points = 10) {
      |                ^
      |         (          )
main.c:6:16: note: use '==' to turn this assignment into an equality comparison
    6 |     if (points = 10) {
      |                ^
      |                ==
1 error generated.

points = 10 in the condition doesn't compare anything. It would store 10 in points and then use that 10 as the condition, which counts as true. The block would run every time, whatever points held before. Elsewhere, C compilers usually let this through with a warning at most, and the program then quietly does the wrong thing. Here it's an error, so you find out right away. (The end of the first line, in square brackets, just names the check that found the problem.)

Below the error, the compiler adds two notes, lines with note:, that suggest fixes. The second one is the right one here: use ==. The first one, about parentheses, is for the rare case when someone really wants to store a value inside a condition, so don't follow it.

You can compare characters as well as numbers. A character goes in single quotes:

char size = 'M';

if (size == 'M') {
    printf("We have your size\n");
}

This prints:

We have your size

Upper and lower case letters are different characters, so 'm' == 'M' is false. Comparing whole texts works differently in C; you'll learn how in the lesson on strings.

Be careful with == and decimal numbers. The computer stores most of them only approximately, so even 0.1 + 0.2 == 0.3 is false. With a double, compare with <, >, <= or >= instead.

Instructions

  1. 1

    An online shop gives a gift to its 100th customer. Run the program and read the first line of the error: it tells you where the problem is and what it is. Fix the condition so that it compares. Then the program prints Customer number 57.

  2. 2

    The shop delivers for free when an order costs 1000 or more. Below the last printf, create an int variable order_total with the value 1000, and write an if statement with an else branch: if order_total is at least 1000, print Free delivery, otherwise print Delivery costs 99. An order of exactly 1000 gets free delivery too, so choose the operator carefully. The program prints:

    Customer number 57
    Free delivery
Back

4/13

  1. 1. The if Statement
  2. 2. Blocks and Indentation
  3. 3. The else Branch
  4. 4. Comparison Operators
  5. 5. True and False
  6. 6. More Options: else if
  7. 7. Quiz: else if
  8. 8. Logical Operators: and
  9. 9. Logical Operators: or
  10. 10. Logical Operators: not
  11. 11. Nested Conditions
  12. 12. The switch Statement
  13. 13. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.