views:

60

answers:

1

I've got the following printf statement:

printf("val=%-4.2lf", val);

However, val is never padded with spaces so the space taken up by val is different if there are 3 or 4 digits before the decimal. Shouldn't the 4 in the format specifier guarantee that there are at least 4 spaces?

Sorry for the newb question but it's been frustrating me.

+4  A: 

The 4 specifies the minimum total field with - you also have the decimal point and the 2 digits after the decimal that are taking a minimum of 3 characters out of that 4 character field width.

So if you want a minimum field width of 4 characters before the decimal point, you'd want to use a format of %-7.2lf.

Michael Burr
You are the man! Thanks!
Tyler Brock