What will be the output of the following C program?
int a[] = {5,10,15,20,25};
printf("%d", sizeof(a) / sizeof(a[0]));
Options
-
A.
4
-
B.
5
-
C.
20
-
D.
More than one of the above
-
E.
None of the above
Correct Answer (Detailed Explanation is Below)
B.
5
Detailed Explanation
Correct Answer: 5
The array contains five integer elements: 5, 10, 15, 20, 25.
sizeof(a) gives the total size occupied by the complete array. sizeof(a[0]) gives the size of one element.
Dividing the total array size by the size of one element gives the number of elements:
sizeof(a) / sizeof(a[0]) = 5
This is a common technique used in C to calculate the number of elements in an array.