Bird
0
0

Why does this code print unexpected output?

hard📝 Conceptual Q10 of 15
C - Input and Output
Why does this code print unexpected output?
int num = 7;
printf("Number: %f", num);
AMissing semicolon after printf
BVariable num is not initialized
CUsing %f to print an int causes undefined behavior
Dprintf cannot print integers
Step-by-Step Solution
Solution:
  1. Step 1: Check format specifier and variable type mismatch

    The code uses %f (float) to print an int variable, which is incorrect and causes undefined behavior.
  2. Step 2: Understand consequences of mismatch

    This mismatch can print garbage values or cause runtime errors because printf expects a float argument but receives an int.
  3. Final Answer:

    Using %f to print an int causes undefined behavior -> Option C
  4. Quick Check:

    Match format specifier to variable type exactly [OK]
Quick Trick: Always match % specifier to variable type in printf [OK]
Common Mistakes:
  • Using wrong specifier
  • Assuming implicit conversion
  • Ignoring compiler warnings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes