ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
#define INCR(x) (++x)
int arr[3] = {10, 20, 30};
int i = 0;
printf("%d", arr[INCR(i)]);
  1. A. 20
  2. B. 10
  3. C. Undefined behaviour
  4. D. 30

Correct answer: A. 20

INCR(i) expands to (++i), which increments i to 1 and evaluates to 1 (no double-use of i here since the macro only references x once), so arr[INCR(i)] becomes arr[(++i)] = arr[1] = 20. There is no undefined behaviour since i is modified exactly once in this expression.

More C Programming questions

Exams that ask this topic