Understanding printf() and its Formatting Options

  • Thread starter Thread starter transgalactic
  • Start date Start date
AI Thread Summary
The discussion centers on the format specifier used in the printf function, specifically "%05.5f". This specifier indicates that a floating-point number should be printed with a total width of at least five characters, including leading zeros if necessary, and with exactly five digits after the decimal point. The first "5" denotes the minimum width, while the second "5" specifies the precision after the decimal. Additionally, a space after a comma in the printf statement is noted to be output directly, as per the formatting rules. Participants reference external resources for further clarification on printf formatting and confirm the output of the printf line, which demonstrates the specified formatting.
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:
Technology 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
 
Yes, you are right.
 
there is a space after a comma
what this thing does?
 
  • #10
it says there

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

but in my printf i get the flag at the last place
 
  • #11
Back
Top