ProgrammingHardWipro · Cognizant pattern

What can be said about this program?

What can be said about this program?
int main(void) {
    int arr[5];
    arr[5] = 10;
    printf("%d", arr[5]);
}
  1. A. It is well-defined and simply writes past the array into adjacent padding bytes
  2. B. Accessing arr[5] is undefined behavior: valid indices for arr are 0-4, so the program may print 10, corrupt other data, or crash
  3. C. It is a compile-time error because the array bound is known at compile time
  4. D. It always prints 0 because C zero-fills memory just past an array

Correct answer: B. Accessing arr[5] is undefined behavior: valid indices for arr are 0-4, so the program may print 10, corrupt other data, or crash

arr has 5 elements, indices 0 through 4; arr[5] is out of bounds. Out-of-bounds array access is undefined behavior with no guaranteed outcome, even if no immediate crash is observed.

More C Programming questions

Exams that ask this topic