A

Akash Lawaniya • 9.58K Points
Tutor III

Q. What will be the output of the following C code?

Code:
#include 
    printf("%.0f", 4.89);
  • (A) 4.890000
  • (B) 4.89
  • (C) 4
  • (D) 5

Explanation by: Mr. Dubey

%.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.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.


Question analytics