In the following C program, what will be printed for the sizes of the pointer variables better and best (assume a 32-bit system)? #include <stdio.h> void main() { char good, *better, *best; printf("%d..%d", sizeof(better), sizeof(best)); }

Difficulty: Medium

Correct Answer: 4..4

Explanation:


Introduction / Context:
This question focuses on how the sizeof operator behaves with pointers in C. It is important to understand that the size of a pointer type depends on the machine architecture (for example, 32-bit or 64-bit), not on the type of data it points to. Here we explicitly assume a 32-bit system where pointers are typically 4 bytes wide.


Given Data / Assumptions:

    • good is a variable of type char.

    • better and best are variables of type char * (pointers to char).

    • The program prints sizeof(better) and sizeof(best) using "%d..%d".

    • We assume a 32-bit platform where all data pointers are 4 bytes.

    • The computation does not depend on the value stored in the pointers.



Concept / Approach:
In C, sizeof applied to a pointer variable gives the size of the pointer itself, not the size of the object it points to. On a 32-bit system, a typical data pointer (such as char *, int * or double *) occupies 4 bytes, regardless of the pointee type. Therefore sizeof(better) and sizeof(best) both yield 4. The printed output combines these two integers with two dots between them.


Step-by-Step Solution:
Step 1: better is declared as char *better;, so it is a pointer variable storing an address. Step 2: best is declared as char *best; and is also a pointer variable storing an address. Step 3: On a 32-bit system, the size of any data pointer is typically 4 bytes, so sizeof(better) = 4 and sizeof(best) = 4. Step 4: The printf call prints these sizes with the format "%d..%d", so the exact output becomes 4..4. Step 5: There is no dependency on the values stored in good, better or best; only their types matter for sizeof.


Verification / Alternative check:
If you changed the declarations to int *p; double *q; and printed sizeof(p) and sizeof(q) on the same 32-bit system, you would still get 4..4. This confirms that pointer size is determined by the architecture, not by the type pointed to. On a 64-bit system, you might instead see 8..8, but the question fixes the assumption to a 32-bit platform, leading to 4..4.


Why Other Options Are Wrong:
Option A (1..2) incorrectly suggests that pointer size depends on the underlying data type or that they differ in width, which is not true for standard data pointers on a 32-bit system. Option C (4..2) mixes two different sizes without justification. Option D (2..2) reflects an older 16-bit environment where pointers could be 2 bytes, but this contradicts the more common 32-bit assumption used in such exam questions.


Common Pitfalls:
A common mistake is to confuse sizeof(pointer) with sizeof(*pointer). The former gives the size of the pointer itself, while the latter gives the size of the pointee type (for example, sizeof(*better) would be 1 because it is a char). Another pitfall is assuming that pointer size changes with the pointee type, which is usually not the case on mainstream architectures. Always remember to consider the platform word size when reasoning about pointer sizes in interview or exam questions.


Final Answer:
On a 32-bit system, both char pointers occupy 4 bytes, so the program prints 4..4.

More Questions from Programming

Discussion & Comments

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