ProgrammingVery hardProduct company OA

What is printed?

What is printed?
int x = 5;
#define x (x + 1)
printf("%d", x);
  1. A. 5
  2. B. 7
  3. C. Nothing: the preprocessor loops forever
  4. D. 6

Correct answer: D. 6

A macro's name is not re-expanded inside its own replacement, so x becomes (x + 1) once, and that inner x is the variable. The declaration int x = 5 came before the #define and is unaffected. Self-reference is how such macros avoid infinite expansion.

More C Programming questions

Exams that ask this topic