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]);- A.
1
- B.
5
- C.
Undefined behavior
- 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.