what is %f doing in C? [closed]
If I use a conversion specifier like %.2f
to print a floating point number, it limits the floating point to 2 decimal places when it's printed.
But using a formation like %2f
to a float doesn't seem to be doing anything, what am I missing?
c floating-point printf
closed as off-topic by DevSolar, Antti Haapala, usr2564301, P.P., gnat Nov 14 '18 at 17:21
- This question does not appear to be about programming within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
If I use a conversion specifier like %.2f
to print a floating point number, it limits the floating point to 2 decimal places when it's printed.
But using a formation like %2f
to a float doesn't seem to be doing anything, what am I missing?
c floating-point printf
closed as off-topic by DevSolar, Antti Haapala, usr2564301, P.P., gnat Nov 14 '18 at 17:21
- This question does not appear to be about programming within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.
1
Have you read the man page? There, it's described as the minimum field width (hint: try using a larger number, such as 15, if you want to see much difference).
– Toby Speight
Nov 14 '18 at 16:18
Why is this not enough to answer your question?
– Quentin
Nov 14 '18 at 16:18
4
I'm voting to close this question as its answer is a trivial documentation lookup.
– DevSolar
Nov 14 '18 at 16:23
1
You're missing that%2f
says simultaneously 'at least two characters of output' and '6 decimal places of output' (by default, because there was no.n
to override it). And, since it must print 7 characters, the 'minimum of 2' is meaningless. Increase 2 to 20 and you'd see an effect.
– Jonathan Leffler
Nov 14 '18 at 17:43
add a comment |
If I use a conversion specifier like %.2f
to print a floating point number, it limits the floating point to 2 decimal places when it's printed.
But using a formation like %2f
to a float doesn't seem to be doing anything, what am I missing?
c floating-point printf
If I use a conversion specifier like %.2f
to print a floating point number, it limits the floating point to 2 decimal places when it's printed.
But using a formation like %2f
to a float doesn't seem to be doing anything, what am I missing?
c floating-point printf
c floating-point printf
edited Nov 14 '18 at 16:27
Sourav Ghosh
110k14130188
110k14130188
asked Nov 14 '18 at 16:14
KrishKrish
37227
37227
closed as off-topic by DevSolar, Antti Haapala, usr2564301, P.P., gnat Nov 14 '18 at 17:21
- This question does not appear to be about programming within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by DevSolar, Antti Haapala, usr2564301, P.P., gnat Nov 14 '18 at 17:21
- This question does not appear to be about programming within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.
1
Have you read the man page? There, it's described as the minimum field width (hint: try using a larger number, such as 15, if you want to see much difference).
– Toby Speight
Nov 14 '18 at 16:18
Why is this not enough to answer your question?
– Quentin
Nov 14 '18 at 16:18
4
I'm voting to close this question as its answer is a trivial documentation lookup.
– DevSolar
Nov 14 '18 at 16:23
1
You're missing that%2f
says simultaneously 'at least two characters of output' and '6 decimal places of output' (by default, because there was no.n
to override it). And, since it must print 7 characters, the 'minimum of 2' is meaningless. Increase 2 to 20 and you'd see an effect.
– Jonathan Leffler
Nov 14 '18 at 17:43
add a comment |
1
Have you read the man page? There, it's described as the minimum field width (hint: try using a larger number, such as 15, if you want to see much difference).
– Toby Speight
Nov 14 '18 at 16:18
Why is this not enough to answer your question?
– Quentin
Nov 14 '18 at 16:18
4
I'm voting to close this question as its answer is a trivial documentation lookup.
– DevSolar
Nov 14 '18 at 16:23
1
You're missing that%2f
says simultaneously 'at least two characters of output' and '6 decimal places of output' (by default, because there was no.n
to override it). And, since it must print 7 characters, the 'minimum of 2' is meaningless. Increase 2 to 20 and you'd see an effect.
– Jonathan Leffler
Nov 14 '18 at 17:43
1
1
Have you read the man page? There, it's described as the minimum field width (hint: try using a larger number, such as 15, if you want to see much difference).
– Toby Speight
Nov 14 '18 at 16:18
Have you read the man page? There, it's described as the minimum field width (hint: try using a larger number, such as 15, if you want to see much difference).
– Toby Speight
Nov 14 '18 at 16:18
Why is this not enough to answer your question?
– Quentin
Nov 14 '18 at 16:18
Why is this not enough to answer your question?
– Quentin
Nov 14 '18 at 16:18
4
4
I'm voting to close this question as its answer is a trivial documentation lookup.
– DevSolar
Nov 14 '18 at 16:23
I'm voting to close this question as its answer is a trivial documentation lookup.
– DevSolar
Nov 14 '18 at 16:23
1
1
You're missing that
%2f
says simultaneously 'at least two characters of output' and '6 decimal places of output' (by default, because there was no .n
to override it). And, since it must print 7 characters, the 'minimum of 2' is meaningless. Increase 2 to 20 and you'd see an effect.– Jonathan Leffler
Nov 14 '18 at 17:43
You're missing that
%2f
says simultaneously 'at least two characters of output' and '6 decimal places of output' (by default, because there was no .n
to override it). And, since it must print 7 characters, the 'minimum of 2' is meaningless. Increase 2 to 20 and you'd see an effect.– Jonathan Leffler
Nov 14 '18 at 17:43
add a comment |
1 Answer
1
active
oldest
votes
This is the minimum field width. Quoting C11
, chapter §7.21.6.1
[...] minimum field width. If the converted value has fewer characters than the
field width, it is padded with spaces (by default) on the left (or right, if the left
adjustment flag, described later, has been given) to the field width. [....]
I ran a test program, just to understand
printf("%10fn", 20.56);
printf("%fn", 20.56);
the output is
20.560000
20.560000
For a better understanding:
printf("%06.2fn", 2.56); // zero padding, instead of space, limited the precision
printf("%0.2fn", 2.56); // to be only two digits, so, total width to be printed
// is 4, and in first case, we take the minimum width to be 6
// so, on left, 2 digit padding should be there
result:
002.56
2.56
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is the minimum field width. Quoting C11
, chapter §7.21.6.1
[...] minimum field width. If the converted value has fewer characters than the
field width, it is padded with spaces (by default) on the left (or right, if the left
adjustment flag, described later, has been given) to the field width. [....]
I ran a test program, just to understand
printf("%10fn", 20.56);
printf("%fn", 20.56);
the output is
20.560000
20.560000
For a better understanding:
printf("%06.2fn", 2.56); // zero padding, instead of space, limited the precision
printf("%0.2fn", 2.56); // to be only two digits, so, total width to be printed
// is 4, and in first case, we take the minimum width to be 6
// so, on left, 2 digit padding should be there
result:
002.56
2.56
add a comment |
This is the minimum field width. Quoting C11
, chapter §7.21.6.1
[...] minimum field width. If the converted value has fewer characters than the
field width, it is padded with spaces (by default) on the left (or right, if the left
adjustment flag, described later, has been given) to the field width. [....]
I ran a test program, just to understand
printf("%10fn", 20.56);
printf("%fn", 20.56);
the output is
20.560000
20.560000
For a better understanding:
printf("%06.2fn", 2.56); // zero padding, instead of space, limited the precision
printf("%0.2fn", 2.56); // to be only two digits, so, total width to be printed
// is 4, and in first case, we take the minimum width to be 6
// so, on left, 2 digit padding should be there
result:
002.56
2.56
add a comment |
This is the minimum field width. Quoting C11
, chapter §7.21.6.1
[...] minimum field width. If the converted value has fewer characters than the
field width, it is padded with spaces (by default) on the left (or right, if the left
adjustment flag, described later, has been given) to the field width. [....]
I ran a test program, just to understand
printf("%10fn", 20.56);
printf("%fn", 20.56);
the output is
20.560000
20.560000
For a better understanding:
printf("%06.2fn", 2.56); // zero padding, instead of space, limited the precision
printf("%0.2fn", 2.56); // to be only two digits, so, total width to be printed
// is 4, and in first case, we take the minimum width to be 6
// so, on left, 2 digit padding should be there
result:
002.56
2.56
This is the minimum field width. Quoting C11
, chapter §7.21.6.1
[...] minimum field width. If the converted value has fewer characters than the
field width, it is padded with spaces (by default) on the left (or right, if the left
adjustment flag, described later, has been given) to the field width. [....]
I ran a test program, just to understand
printf("%10fn", 20.56);
printf("%fn", 20.56);
the output is
20.560000
20.560000
For a better understanding:
printf("%06.2fn", 2.56); // zero padding, instead of space, limited the precision
printf("%0.2fn", 2.56); // to be only two digits, so, total width to be printed
// is 4, and in first case, we take the minimum width to be 6
// so, on left, 2 digit padding should be there
result:
002.56
2.56
edited Nov 14 '18 at 16:25
answered Nov 14 '18 at 16:18
Sourav GhoshSourav Ghosh
110k14130188
110k14130188
add a comment |
add a comment |
1
Have you read the man page? There, it's described as the minimum field width (hint: try using a larger number, such as 15, if you want to see much difference).
– Toby Speight
Nov 14 '18 at 16:18
Why is this not enough to answer your question?
– Quentin
Nov 14 '18 at 16:18
4
I'm voting to close this question as its answer is a trivial documentation lookup.
– DevSolar
Nov 14 '18 at 16:23
1
You're missing that
%2f
says simultaneously 'at least two characters of output' and '6 decimal places of output' (by default, because there was no.n
to override it). And, since it must print 7 characters, the 'minimum of 2' is meaningless. Increase 2 to 20 and you'd see an effect.– Jonathan Leffler
Nov 14 '18 at 17:43