Difficulty: Easy
Correct Answer: 4, 1
Explanation:
Introduction / Context:
This problem focuses on the behavior of sizeof
with NULL
and with an empty string literal ""
in C. The key ideas are how NULL
is defined on your platform and how string literals are stored in memory.
Given Data / Assumptions:
NULL
is a null pointer constant and commonly defined as ((void*)0)
or 0
depending on headers.""
is an array of char containing only the terminating \0
, so its sizeof
is 1.
Concept / Approach:
If NULL
is defined as ((void*)0)
, then sizeof(NULL)
is the size of a void*
, which equals 4 bytes on this system. The empty string literal is a statically allocated array of length 1 (containing just the terminator), so sizeof("")
returns 1.
Step-by-Step Solution:
Assume sizeof(void*) = 4 → sizeof(NULL) = 4.An empty literal contains only '\0' → sizeof("") = 1.Therefore the program prints: 4, 1.
Verification / Alternative check:
On 64-bit platforms, sizeof(void*)
is typically 8; the first value would change accordingly. The second value remains 1 on all systems.
Why Other Options Are Wrong:
Any option with 2 for the first value contradicts the pointer-size assumption. Any option with 2 for the second value is incorrect because an empty string literal is exactly one byte long due to the terminator.
Common Pitfalls:
Confusing strlen("") == 0
with sizeof("") == 1
. Remember, sizeof
counts the terminator for string literals.
Final Answer:
4, 1
Discussion & Comments