ProgrammingVery hardProduct company OA

What is printed?

What is printed?
enum { RED = 2, GREEN, BLUE = GREEN + 4, N };
int a[] = { [RED] = 5, [BLUE] = 1, 9, [GREEN] = 6, 4 };
printf("%zu %d %d %d %d", sizeof a / sizeof a[0], a[4], a[8], a[5], N);
  1. A. 8 4 0 0 8
  2. B. 5 4 9 0 4
  3. C. 9 0 9 4 8
  4. D. 9 4 9 0 8

Correct answer: D. 9 4 9 0 8

Enumerators continue from the previous value: GREEN = 3, BLUE = 7, N = 8. In an initialiser, an element without a designator goes to the position right AFTER the previous one: 9 follows [BLUE] and lands in a[8]; 4 follows [GREEN] and lands in a[4]. The array size is the highest initialised index + 1 = 9, and untouched elements such as a[5] are zero. '8 …' assumes the size is N.

More C Programming questions

Exams that ask this topic