Friday, June 25, 2010

C printf formatting floats

Question :
printf ("floats: %4.2f\n", 3.1416);

In 4.2f above, what does the 4 stand for?
Neither there are 3 spaces left before 3.14 in the output
nor do I see any change if change 3.1416 to 12345.1416
answer :
4 is the total minimum field width. 2 is the precision (2 digits right
of the decimal)

If the number is larger than the 4 specificied, it will take up more
than 4 characters, as you found when using the larger number. It is a
minimum.

No comments:

Blog Archive