ProgrammingHardProduct-company MCQ pattern

What happens when this runs?

What happens when this runs?
int main(void) {
    int arr[3];
    for (int i = 0; i < 3; i++) {
        printf("%d ", arr[i]);
    }
}
  1. A. 0 0 0, since local arrays are always zero-initialised
  2. B. Undefined behaviour: arr is an uninitialised automatic array; every element has an indeterminate value
  3. C. Garbage values, but guaranteed to be the same three values on every run
  4. D. Compile-time error: arr must be initialised

Correct answer: B. Undefined behaviour: arr is an uninitialised automatic array; every element has an indeterminate value

Automatic (non-static, local) arrays are not zero-initialised unless explicitly given an initializer; each element holds an indeterminate value from whatever was previously in that stack memory. Reading any of them before writing is undefined behaviour, and the values may differ between runs, compilers, or optimisation levels.

More C Programming questions

Exams that ask this topic