Understanding printf() and its Formatting Options

  • Thread starter Thread starter transgalactic
  • Start date Start date
Click For Summary

Discussion Overview

The discussion focuses on understanding the formatting options of the printf function in C, specifically the meaning of the format specifier %05.5f and the implications of including a space after a comma in the output. Participants explore both theoretical aspects and practical examples related to formatting floating-point numbers.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • Some participants explain that %05.5f indicates a total field width of 5 characters, with zero padding, and that the last 5 specifies the number of digits after the decimal point.
  • Others clarify that the first "5" ensures at least five characters are printed, while the second "5" specifies precision to five decimal places.
  • A participant shares an output example from their printf statement, questioning the visibility of the expected format in the output.
  • There is a discussion about the presence of a space after a comma in the printf statement, with some stating it outputs a space directly.
  • One participant notes that the order of flags in the format specifier may differ from what is described in references, prompting further exploration of the printf format documentation.

Areas of Agreement / Disagreement

Participants generally agree on the meanings of the format specifiers, but there are some uncertainties regarding the order of flags and how they are applied in practice. The discussion remains somewhat unresolved regarding the implications of the space after the comma.

Contextual Notes

Some participants reference external sources for clarification, indicating potential limitations in their understanding of the printf formatting rules. There is also a lack of consensus on the exact behavior of the format specifiers in different contexts.

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

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
47
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
17K
  • · Replies 1 ·
Replies
1
Views
11K
Replies
6
Views
4K