Bird
0
0

The following code tries to print the size of a short variable but causes a compilation error:

medium📝 Debug Q14 of 15
C - Variables and Data Types
The following code tries to print the size of a short variable but causes a compilation error:
short s;
printf("%d", sizeof(s));

What is the main reason for the error?
AVariable <code>s</code> is not initialized
BMissing semicolon after <code>short s</code>
CCannot use <code>sizeof</code> on variables
DUsing wrong format specifier for <code>sizeof</code> result
Step-by-Step Solution
Solution:
  1. Step 1: Identify the type returned by sizeof

    The sizeof operator returns a value of type size_t, which is an unsigned integer type.
  2. Step 2: Check printf format specifier compatibility

    The format specifier %d expects an int, but sizeof returns size_t. The correct specifier is %zu.
  3. Final Answer:

    Using wrong format specifier for sizeof result -> Option D
  4. Quick Check:

    Use %zu for sizeof output [OK]
Quick Trick: Use %zu to print sizeof results safely [OK]
Common Mistakes:
  • Using %d instead of %zu for sizeof
  • Thinking sizeof needs variable initialization
  • Missing semicolon (actually present)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes