What is the output of the following C code snippet? int arr[] = {1, 2, 3, 4,…

2024

What is the output of the following C code snippet?

int arr[] = {1, 2, 3, 4, 5};
printf("%d", arr[5]);
  1. A.

    1

  2. B.

    5

  3. C.

    Undefined behavior

  4. D.

    0

Attempted by 855 students.

Show answer & explanation

Correct answer: C

Correct answer: Undefined behavior

  • Array size: The array has 5 elements.

  • Valid indexes: In C, indexing starts from 0, so the valid indexes are 0, 1, 2, 3, and 4.

  • Out-of-bounds access: The expression arr[5] tries to read the sixth position, which is outside the array bounds.

  • Result: C does not define a fixed output for this access; it may print garbage, crash, or appear to work accidentally.

Therefore the behavior is undefined, not a specific numeric output.

Explore the full course: Cocubes Preparation