What is the output of the following code snippet? int arr[] = {1, 2, 3, 4, 5};…
2024
What is the output of the following code snippet?
int arr[] = {1, 2, 3, 4, 5};
printf("%d", arr[5]);
- A.
1
- B.
5
- C.
Undefined behaviour
- D.
0
Attempted by 926 students.
Show answer & explanation
Correct answer: C
The code tries to access arr[5], which is out of bounds for the array declared. Arrays in C do not perform boundary checking, so trying to access outside its limit leads to undefined behavior, which could be anything like garbage value or a segmentation fault.