Find the output of the following C program. (Assume the program is compiled…
2026
Find the output of the following C program. (Assume the program is compiled for a 32-bit system, where every pointer occupies 4 bytes.)
int main()
{
char arr[] = {1, 2, 3};
char *p = arr;
printf(" %d ", sizeof(p));
printf(" %d ", sizeof(arr));
getchar();
}
Answer: A. 4 3 — Concept: sizeof(array) always evaluates to the array's TOTAL byte size — number of elements multiplied by the size of one element — because the array's own…
- A.
4 3
- B.
3 3
- C.
0 0
- D.
2 3
Attempted by 281 students.
Show answer & explanation
Correct answer: A
Concept: sizeof(array) always evaluates to the array's TOTAL byte size — number of elements multiplied by the size of one element — because the array's own storage is what is measured. sizeof(pointerVariable), by contrast, always evaluates to the fixed size of a pointer on the target machine (for example 4 bytes on a 32-bit system, 8 bytes on a 64-bit system), regardless of what the pointer points to — a pointer variable only ever stores a memory address.
arr is declared as char arr[] = {1, 2, 3};, giving it exactly 3 elements of type char (1 byte each), so sizeof(arr) = 3 × 1 = 3.
p is declared as char *p = arr;, so p is a pointer variable holding the address of arr[0]. sizeof(p) measures the pointer variable itself, not what it points to.
On the 32-bit system assumed by this question, every pointer occupies 4 bytes, so sizeof(p) = 4.
The two printf statements print sizeof(p) and then sizeof(arr), in that order, giving the output 4 3.
Cross-check: This matches option '4 3'. The two sizes measure different things — sizeof(p) is a fixed platform constant (4 bytes here), while sizeof(arr) scales with the number of array elements (3 here) — which is exactly why the two values differ even though p was initialized from arr.