Difficulty: Medium
Correct Answer: 12
Explanation:
Introduction / Context:
This question tests understanding of the sizeof operator and the types of character literals in C, especially in the context of older 16-bit compilers. While a char variable clearly has size 1 byte, a character literal such as 'A' may have type int rather than char, causing sizeof('A') to differ from sizeof(ch). Recognizing this distinction is important for writing portable code and for interpreting sizeof results correctly.
Given Data / Assumptions:
Concept / Approach:
In C, character literals like 'A' are of type int, not char, unless a specific wide or other literal notation is used. Therefore sizeof('A') is actually sizeof(int). On a 16-bit system, int is typically 2 bytes. On the other hand, sizeof(ch) is the size of a char, which is 1 byte. When printf prints these two values back to back with %d%d, the resulting output will be the digits 1 and 2 with no space in between.
Step-by-Step Solution:
Step 1: Determine sizeof(ch). Because ch is of type char, sizeof(ch) = 1 byte on the given system.Step 2: Determine the type of the literal 'A'. In standard C, a plain character literal like 'A' has type int.Step 3: On a 16-bit compiler, sizeof(int) = 2 bytes, so sizeof('A') = 2.Step 4: The printf call uses "%d%d", so it prints the first integer (1) immediately followed by the second integer (2).Step 5: Therefore, the visible output is the two-digit sequence 12.
Verification / Alternative check:
On many compilers, you can test the types by using expressions like if (sizeof('A') == sizeof(int)) or using typeof extensions. The C standard confirms that character constants like 'A' are of type int. Running a small test program that prints sizeof(char) and sizeof('A') will typically show 1 and 2 on older 16-bit systems, confirming the analysis.
Why Other Options Are Wrong:
Option B (11) would require both sizeof(ch) and sizeof('A') to be 1, which is not the case when 'A' has type int. Option C (21) and Option D (22) mismatch at least one of the sizes and do not respect the given data about char and int sizes. The correct combination for this environment is 1 and 2, yielding 12.
Common Pitfalls:
Many programmers mistakenly assume that character literals are of type char. This can lead to surprises in sizeof expressions and in function overloading in C++. Another pitfall is forgetting that printf("%d%d", ...) prints values without spaces; if you want "1 2" you must write "%d %d". Understanding literal types and format strings helps avoid such confusion.
Final Answer:
The program prints 12.
Discussion & Comments