Subject: Searching for Clarification on Using “printf() flags #
”
Hello,
I am currently researching the use of “printf() flags #
” in C programming. I refer to four different sources for information: cplusplus.com, en.cppreference.com, libc.pdf (especially section 12.12.5 on page 296) and C11 standard (section 7.21.6.1).
Regarding the use of “printf() flags #
” I found the following statement:
“For a, A, e, E, E, f, F, F, F, g, and G
conversions, the result of converting a floating-point number always contains a decimal point character, even if it is not followed by any digits. (Normally, a decimal point character appears in the result of these conversions only if it is followed by a digit). For g and G conversions, trailing zeros are not removed from the result. For other conversions, the behavior is undefined.”
Although I have tried to understand this statement by experimenting with examples, I am still struggling to grasp its meaning. Therefore, I am reaching out to you to ask for your help. Could you please review my examples and help me understand?
Thank you in advance for your help.
Sincerely yours,
The usage of printf() flags #
. Which statement is correct? Is it just my 'flags #'
code lines, or my 'flags # precision.'
code lines? Below are my examples.
code input:
printf("//////////////// printf format specifier [f] [ # ]flags examples *******************n");
printf(" default = [%f]n",1.234567);
printf(" default = [%f]n",1.234567);
printf(" flags # = [%#f]nn",1.234567);
printf(" flags # = [%#f]nn",1234567.);
printf(" flags # precison . = [%#.f]nn",1.234567);
printf(" flags # precison . = [%#.f]nn",1234567.);
printf("//////////////// printf format specifier [e] [ # ]flags examples *******************n");
printf(" default = [%e]n",1.234567);
printf(" default = [%e]n",1234567.);
printf(" flags # = [%#e]nn",1.234567);
printf(" flags # = [%#e]nn",1234567.);
printf(" flags # precison . = [%#.e]nn",1.234567);
printf(" flags # precison . = [%#.e]nn",1234567.);
printf("//////////////// printf format specifier [g] [ #) ]flags examples *******************n");
printf(" default = [%g]n",1.234567);
printf(" default = [%g]n",1234567.);
printf(" flags # = [%#g]nn",1.234567);
printf(" flags # = [%#g]nn",1234567.);
printf(" flags # precison . = [%#.g]nn",1.234567);
printf(" flags # precison . = [%#.g]nn",1234567.);
printf("//////////////// printf format specifier [a] [ # ]flags examples *******************n");
printf(" default = [%a]n",1.234567);
printf(" default = [%a]n",1234567.);
printf(" flags # = [%#a]nn",1.234567);
printf(" flags # = [%#a]nn",1234567.);
printf(" flags # precison . = [%#.a]nn",1.234567);
printf(" flags # precison . = [%#.a]nn",1234567.);
code output:
//////////////// printf format specifier [f] [ # ]flags examples *******************
default = [1.234567]
default = [1.234567]
flags # = [1.234567]
flags # = [1234567.000000]
flags # precison . = [1.]
flags # precison . = [1234567.]
//////////////// printf format specifier [e] [ # ]flags examples *******************
default = [1.234567e+000]
default = [1.234567e+006]
flags # = [1.234567e+000]
flags # = [1.234567e+006]
flags # precison . = [1.e+000]
flags # precison . = [1.e+006]
//////////////// printf format specifier [g] [ #) ]flags examples *******************
default = [1.23457]
default = [1.23457e+006]
flags # = [1.23457]
flags # = [1.23457e+006]
flags # precison . = [1.]
flags # precison . = [1.e+006]
//////////////// printf format specifier [a] [ # ]flags examples *******************
default = [0x1.3c0c95p+0]
default = [0x1.2d6870p+20]
flags # = [0x1.3c0c95p+0]
flags # = [0x1.2d6870p+20]
flags # precison . = [0x1.p+0]
flags # precison . = [0x1.p+20]
1