In C on a typical 32-bit model (char=1 byte, int=4 bytes, float=4 bytes), what does sizeof report?
#include
int main()
{
char ch = 'A';
printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14f));
return 0;
}
-
A1, 2, 4
-
B1, 4, 4
-
C2, 2, 4
-
D2, 4, 8
-
E1, 1, 4
Answer
Correct Answer: 1, 4, 4
Explanation
Introduction / Context:This problem checks your knowledge of the sizeof operator in C, particularly how it treats variables, character constants, and floating constants under a typical 32-bit data model where char=1 byte, int=4 bytes, and float=4 bytes.
Given Data / Assumptions:
- char occupies 1 byte.
- int occupies 4 bytes.
- float occupies 4 bytes.
- The code prints three sizeof results in order: sizeof(ch), sizeof('A'), sizeof(3.14f).
Concept / Approach:sizeof(variable) yields the storage size of that object’s type. In C, a character constant like 'A' has type int (not char), so sizeof('A') equals sizeof(int). A float constant with suffix f has type float.
Step-by-Step Solution:sizeof(ch) = 1 (because ch is char).sizeof('A') = sizeof(int) = 4 (character constants are of type int).sizeof(3.14f) = sizeof(float) = 4.Printed result: 1, 4, 4.
Verification / Alternative check:Replace the character constant with a variable: int x = 'A'; sizeof(x) also returns 4 on this model, reinforcing the rule.
Why Other Options Are Wrong:(a) assumes sizeof('A') is 2, which is nonstandard here. (c) and (d) mismatch the stated data model. (e) incorrectly treats a character constant as a char.
Common Pitfalls:Believing sizeof('A') equals 1 because it “looks like a char”; forgetting the f suffix forces a float (not double).
Final Answer:1, 4, 4