Difficulty: Medium
Correct Answer: 2.000000, 0
Explanation:
Introduction / Context:
This question highlights two concepts: what floor and ceil return (both return double), and the necessity of matching printf format specifiers to argument types. Mismatched specifiers cause undefined behavior.
Given Data / Assumptions:
Concept / Approach:
Because "%d" does not match the double argument, the behavior is undefined by the C standard. On many common ABIs, passing a double where an int is expected often results in printing 0 due to how arguments are placed in registers/stack. The first value prints as 2.000000 correctly.
Step-by-Step Solution:
floor(2.5) = 2.0 → "%f" prints 2.000000.ceil(2.5) = 3.0 → but "%d" reads raw bits as int → undefined; often appears as 0.Thus a commonly observed output is: 2.000000, 0.
Verification / Alternative check:
Correcting the code to printf("%f, %f", floor(i), ceil(i)) would portably print 2.000000, 3.000000. The provided code is intentionally mismatched to test understanding.
Why Other Options Are Wrong:
2, 3 and 2, 0: they do not match the used format string "%f, %d" for the first field.2.000000, 3: would require a correct "%f" for the second field; not the case here.
Common Pitfalls:
Relying on undefined behavior; assuming implicit conversions happen in printf arguments—they do not.
Final Answer:
2.000000, 0
Discussion & Comments