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

C Programming Pointers Difficulty: Easy
Choose an option
  • A
    10, 2, 4
  • B
    20, 4, 4
  • C
    16, 2, 2
  • D
    20, 2, 2
  • E
    24, 4, 4

Answer

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

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