Understanding printf() and its Formatting Options

  • Thread starter Thread starter transgalactic
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
10 replies · 2K views
transgalactic
Messages
1,386
Reaction score
0
what mean this part of the printf

Code:
 printf("calloc1[%d] holds %05.5f, ", i, calloc1[i]);

Code:
%05.5f, "

there is %05.5f
whats that?

and there is a space after a comma
what this thing does?
 
Last edited:
Physics news on Phys.org
You first question: %05.5f means that a total field of 5 characters,and use Zero pad, and the last 5 will hold the decimal part.
Google search give a lot of result:
see here: http://www.cplusplus.com/reference/clibrary/cstdio/printf.html

"a space and a comma" means this will output directly.

Can you test your code?
 
this is th output of the printf line:
Code:
calloc1[0] holds 0.00000, malloc1[0] holds -431602080.00000
i can't see your words in this output

can you mark those part or something?
 
calloc1[0] holds 0.00000
the dot "." For e, E and f specifiers: this is the number of digits to be printed after the decimal point.
so, there are five zeros.
 
ok 0.5 means persition till the 5th place

but there 5.5

whats the other 5 for?
 
The First "5" means : Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.

All the words I copied from the referenced web page I suggest.
 
so the first 5 means that we need to print at least five chars in this float number
and the second 5 means to print the float number in persition 5 place after the point
 
there is a space after a comma
what this thing does?
 
it says there

%[flags][width][.precision][length]specifier

but in my printf i get the flag at the last place