Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

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."

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 (only one)
%f   - floating point (float)
%d   - signed int (decimal)
%i   - signed int (integer) (same as decimal).
%u   - unsigned int
%ld  - long (signed) int
%lu  - long unsigned int
%lld - long long (signed) int
%llu - long long unsigned int

Edit: there are several others listed in @Eli's response (man 3 printf).

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.

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

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top