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
πŸ’¬ Discuss
βœ… 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.
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.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
220
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Akash Lawaniya
Publisher
πŸ“ˆ
93%
Success Rate