Ternary Operator in C

Level 287 keystrokes

Uses the ternary operator to pick "Pass" or "Fail" depending on whether score is at least 60; since score is 75, it prints "Pass".

C code

// Stores the test score 75 in the int variable score
1int score = 75;
// condition ? a : b yields a if the condition is true, otherwise b
// score >= 60 is true, so result points to the string "Pass"
2char *result = (score >= 60) ? "Pass" : "Fail";
// %s prints the string that result points to: "Pass"
3printf("%s\n", result);

Output

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

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

More C examples at level 2

← C typing practice