ProgrammingHardTCS NQT · Infosys pattern

What happens when this runs?

What happens when this runs?
int x;
printf("%d", x);
  1. A. 0, since C always zero-initialises local variables
  2. B. Undefined behaviour: x is an uninitialised automatic variable with an indeterminate value
  3. C. Compile-time error: x must be initialised before use
  4. D. Garbage value, but always the same on every run of the same binary

Correct answer: B. Undefined behaviour: x is an uninitialised automatic variable with an indeterminate value

Automatic (local, non-static) variables are NOT zero-initialised in C; x holds whatever indeterminate bit pattern happened to be in that stack memory. Reading an uninitialised variable like this is undefined behaviour, not just 'garbage but deterministic' — it may even appear to change between runs or optimisation levels.

More C Programming questions

Exams that ask this topic