What happens after%?

I searched for this a bit, but I did not get a particularly direct answer. In C (and I think C ++), how do you determine what happens after% when using printf ?. For instance:

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

I took this example directly from a book written in C. This makes no sense to me. Where did you come up with 10.1f and 10.2f ? Can someone explain this?

+4
source share
8 answers

http://en.wikipedia.org/wiki/Printf#printf_format_placeholders - Wikipedia link for print placeholders. http://www.cplusplus.com/reference/clibrary/cstdio/printf.html is also useful

Mostly in simple form, this is% [width]. [precision] [type]. Width allows you to make sure that the variable being printed has at least a certain length (useful for tables, etc.). Accuracy allows you to specify the accuracy at which a number is printed (for example, decimal digits, etc.), and informs C / C ++ about which variable you specified (character, integer, double, etc.).

Hope this helps

UPDATE:

To clarify the use of your examples:

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

% 10.1f (referring to the first argument: radius) means that it is 10 characters long (i.e. pad with spaces) and prints it as a float with one decimal place.

% 10.2 (with reference to the second argument: region) means that it is 10 characters long (as indicated above) and prints with two decimal places.

+14
source
 man 3 printf 

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

+7
source

10.1f means floating point 10 characters wide with 1 place after the decimal point. If the number has less than 10 digits, it is filled 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 answer (man 3 printf).

+2
source

10.1f means a floating point with 1 place after the decimal point and 10 places before the decimal point. If the number has less than 10 digits, it is filled 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 this best:

Mostly in simple form, this is% [width]. [precision] [type].

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

+2
source

In short, these values ​​after% tell printf how to interpret (or output) all the variables that appear later. In your example, radius interpreted as a float (this is "f"), and 10.1 gives information about how many decimal places to use when printing. A.

For more information on all modifiers that you can use with printf, see this link .

+1
source

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

0
source

Personal pages contain the information you need. To read what you have above:

 printf( "%10.2f", 1.5 ) 

This will print:

  1.50 

While:

 printf("%.2f", 1.5 ) 

Print

 1.50 

Pay attention to the rationale for both. Similarly:

 printf("%10.1f", 1.5 ) 

It will be printed:

  1.5 

Any number after. this is what you want to print. Any number up to. this is the distance from the left edge.

0
source

One problem that has not been raised by others is that double matches float . In some systems, a double format specifier was required for the double compared to the float. Not least because the parameters passed can be of different sizes.

  % f - float
 % lf - double
 % g - double
-1
source

Source: https://habr.com/ru/post/1276380/


All Articles