Bird
0
0

You want to print a floating-point number float pi = 3.14159; with only 2 decimal places. Which printf statement is correct?

hard📝 Application Q15 of 15
C - Input and Output
You want to print a floating-point number float pi = 3.14159; with only 2 decimal places. Which printf statement is correct?
Aprintf("%2f", pi);
Bprintf("%f.2", pi);
Cprintf("%.2f", pi);
Dprintf("%f", pi);
Step-by-Step Solution
Solution:
  1. Step 1: Understand precision in format specifiers

    To limit decimal places, use %.2f where 2 is the number of decimals.
  2. Step 2: Check other options

    %2f sets minimum width, not decimals; %f.2 is invalid; %f prints 6 decimals by default.
  3. Final Answer:

    printf("%.2f", pi); -> Option C
  4. Quick Check:

    Use %.2f to print 2 decimals [OK]
Quick Trick: Use %.2f to print float with 2 decimals [OK]
Common Mistakes:
  • Using %2f expecting 2 decimals
  • Placing .2 after %f incorrectly
  • Using %f prints 6 decimals by default

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes