Ternary Operator in C

Level 266 keystrokes

Uses the ternary operator to take the larger of a (5) and b (10) and prints the maximum, 10.

C code

// The two values to compare: a = 5 and b = 10
1int a = 5, b = 10;
// If a > b the result is a, otherwise b; 5 > 10 is false, so max gets b (10)
2int max = (a > b) ? a : b;
// Prints the maximum, 10
3printf("%d\n", max);

Output

10
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.

  • ;× 3
  • =× 3
  • ,× 2
  • "× 2
  • (× 2
  • )× 2
  • :× 1
  • ?× 1
  • \× 1
  • %× 1

More C examples at level 2

← C typing practice