Q. What will be the output of the following C code?
Code:#include
printf("%.0f", 4.89);
β
Correct Answer: (D)
5
Explanation:
%.0f Format Specifier:
- In printf("%.0f", 4.89);, the .0 means the number will be rounded to zero decimal places (i.e., an integer value).
- The %.0f format rounds the floating-point number to the nearest integer using standard rounding rules.
Rounding Behavior:
- 4.89 is closer to 5 than to 4.
- Since printf("%.0f", 4.89); rounds normally, 4.89 rounds up to 5.
Correct Answer:
β (D) 5
Bonus Notes:
- If you use printf("%.2f", 4.89);, the output will be: 4.89
- If you use printf("%.1f", 4.89);, the output will be: 4.9
- %.0f will always round to the nearest whole number.