In C, using sizeof with arrays and elements (int is 4 bytes): what is printed? #include<stdio.h> int main() { int arr[] = {12, 13, 14, 15, 16}; printf("%d, %d, %d ", sizeof(arr), sizeof(*arr), sizeof(arr[0])); return 0; }

Difficulty: Easy

Correct Answer: 20, 4, 4

Explanation:


Introduction / Context:
This measures your knowledge of sizeof behavior with arrays versus elements in C. Unlike a pointer to the first element, an array expression used directly with sizeof does not decay; it yields the full storage size of the entire array object.


Given Data / Assumptions:

  • int is 4 bytes.
  • arr contains 5 integers: {12, 13, 14, 15, 16}.
  • We compute three sizeof values: the whole array, the first dereferenced element, and arr[0].


Concept / Approach:
sizeof(arr) = number_of_elements * sizeof(int) = 5 * 4 = 20sizeof(*arr) = sizeof(int) = 4sizeof(arr[0]) = sizeof(int) = 4Because sizeof is an operator evaluated at compile time (no decay for arr here), it returns the full byte size of the array object.


Step-by-Step Solution:
Compute each term using int = 4 bytes.Format and print: 20, 4, 4.


Verification / Alternative check:
Replace arr with a pointer variable; then sizeof(ptr) would return the pointer size, not the array size. This highlights why sizeof on arrays is special.


Why Other Options Are Wrong:
(a), (c), (d) use 2-byte ints or wrong counts. (e) assumes 6 elements or different size.


Common Pitfalls:
Confusing arrays with pointers; assuming sizeof(arr) yields pointer size; forgetting that sizeof is compile-time for known objects.


Final Answer:
20, 4, 4

More Questions from Pointers

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion