Difficulty: Medium
Correct Answer: 2.250000e+000, 2.250000, 2.25, 2.250000
Explanation:
Introduction / Context:
This problem checks knowledge of default argument promotions and how printf format specifiers %e, %f, %g, and %lf behave for a float argument passed to printf.
Given Data / Assumptions:
Concept / Approach:
%e prints a floating value in exponential notation with six digits after the decimal by default: “2.250000e+000”. %f prints fixed-point with six digits after the decimal: “2.250000”. %g chooses compact representation (here “2.25”). In printf (unlike scanf), %lf is accepted and treated the same as %f because floats are promoted to double; the l modifier is ignored, so the last output is again “2.250000”.
Step-by-Step Solution:
%e → 2.250000e+000%f → 2.250000%g → 2.25%lf → 2.250000 (treated as %f in printf)
Verification / Alternative check:
Test on common C libraries (glibc, MSVCRT). The C standard allows ignoring l for printf’s floating conversions; scanf differs (%f reads float, %lf reads double).
Why Other Options Are Wrong:
Options with truncated digits or malformed scientific notation ignore printf defaults. “Error” is not correct for a conforming implementation.
Common Pitfalls:
Confusing printf with scanf rules; expecting %lf to fail; forgetting the six-decimal default precision.
Final Answer:
2.250000e+000, 2.250000, 2.25, 2.250000
Discussion & Comments