tags:

views:

352

answers:

8

I've searched for this a little but I have not gotten a particularly straight answer. In C (and I guess C++), how do you determine what comes after the % when using printf?. For example:

double radius = 1.0;
double area = 0.0;
area = calculateArea( radius );
printf( "%10.1f     %10.2\n", radius, area );

I took this example straight from a book that I have on the C language. This does not make sense to me at all. Where do you come up with 10.1f and 10.2f? Could someone please explain this?

+6  A: 
man 3 printf

on a Linux system will give you all the information you need. You can also find these manual pages online, for example at http://linux.die.net/man/3/printf

Eli Courtwright
+12  A: 

http://en.wikipedia.org/wiki/Printf#printf_format_placeholders is Wikipedia's reference for format placeholders in printf. http://www.cplusplus.com/reference/clibrary/cstdio/printf.html is also helpful

Basically in a simple form it's %[width].[precision][type]. Width allows you to make sure that the variable which is being printed is at least a certain length (useful for tables etc). Precision allows you to specify the precision a number is printed to (eg. decimal places etc) and the informs C/C++ what the variable you've given it is (character, integer, double etc).

Hope this helps

UPDATE:

To clarify using your examples:

printf( "%10.1f     %10.2\n", radius, area );

%10.1f (referring to the first argument: radius) means make it 10 characters long (ie. pad with spaces), and print it as a float with one decimal place.

%10.2 (referring to the second argument: area) means make it 10 character long (as above) and print with two decimal places.

robintw
The second format specifier is missing the type...
R..
A: 

10.1f means you want to display a float with 1 decimal and the displayed number should be 10 characters long.

poulejapon
A: 

In short, those values after the % tell printf how to interpret (or output) all of the variables coming later. In your example, radius is interpreted as a float (this the 'f'), and the 10.1 gives information about how many decimal places to use when printing it out.

See this link for more details about all of the modifiers you can use with printf.

Matt Dillard
A: 

Man pages contain the information you want. To read what you have above:

printf( "%10.2f", 1.5 )

This will print:

         1.50

Whereas:

printf("%.2f", 1.5 )

Prints:

1.50

Note the justification of both. Similarly:

printf("%10.1f", 1.5 )

Would print:

        1.5

Any number after the . is the precision you want printed. Any number before the . is the distance from the left margin.

FreeMemory
+1  A: 

10.1f means floating point with 10 characters wide with 1 place after the decimal point. If the number has less than 10 digits, it's padded with spaces. 10.2f is the same, but with 2 places after the decimal point.

You have these basic types:

%d - integer
%x - hex integer
%s - string
%c - char
%f - floating point.
%u - unsigned int
%ul - usigned long int
%l - long int
%ll - long long int
%ull - unsigned long long int

there are others that are listed in @Eli's response

Edit: corrected the meaning of the 10 based on @Gauis' comments below.

Nathan Fellman
There is no such specifier as `%ul` or `%ull` or `%l`; I think you mean `%lu` and `%llu` and `%ld`.
R..
likely. feel free to fix it
Nathan Fellman
+1  A: 

10.1f means floating point with 1 place after the decimal point and the 10 places before the decimal point. If the number has less than 10 digits, it's padded with spaces. 10.2f is the same, but with 2 places after the decimal point.

On every system I've seen, from Unix to Rails Migrations, this is not the case. @robintw expresses it best:

Basically in a simple form it's %[width].[precision][type].

That is, not "10 places before the decimal point," but "10 places, both before and after, and including the decimal point."

James A. Rosen
Behavior of *width* is the same for all format specifiers: it's the minimum total width of the whole formatted field. Limited width will never cause truncation. On the other hand, *precision* has different meanings with different types (strings vs floating point vs integers) and can cause truncation in all cases but integers.
R..
A: 

One issue that hasn't been raised by others is whether double is the same as a float. On some systems a different format specifier was needed for a double compared to a float. Not least because the parameters passed could be of different sizes.

 %f - float
 %lf - double
 %g - double

itj
Nope. Since `printf` is a variadic function, it's subject to default argument type promotions, meaning that it's impossible to pass a `float`. Any `float` argument will get promoted to `double` before being passed. And either `%f` or `%lf` will always work. Also, `%f` vs `%g` has nothing to do with the type of the argument, but with how you want it formatted.
R..