Negative Number Check in C

Level 255 keystrokes

Checks with an if statement whether n is below zero; n is -5, so it prints "Negative: -5".

C code

// An int can hold negative values; n is -5
1int n = -5;
// A number is negative if it is less than 0; -5 < 0 is true
2if (n < 0) {
// %d prints the minus sign too: "Negative: -5"
3 printf("Negative: %d\n", n);
4}

Output

Negative: -5
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • ;× 2
  • "× 2
  • (× 2
  • )× 2
  • -× 1
  • ,× 1
  • :× 1
  • {× 1
  • }× 1
  • \× 1

More C examples at level 2

← C typing practice