ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
enum Color { RED, GREEN, BLUE };
enum Color c = GREEN;
printf("%d", c);
enum Status { OK = 1, FAIL = 5, PENDING };
printf(" %d", PENDING);
  1. A. 1 6
  2. B. 0 6
  3. C. 1 5
  4. D. 2 6

Correct answer: A. 1 6

Unscoped enum constants default to 0,1,2,... unless given explicit values: RED=0, GREEN=1, BLUE=2, so c (GREEN) prints 1. In the second enum, OK=1 (explicit), FAIL=5 (explicit), and PENDING (no explicit value) continues from the PREVIOUS constant + 1: FAIL+1 = 6.

More C Programming questions

Exams that ask this topic