Reputation: 12510
I am trying to understand the following section:
l (ell)
Specifies that a following
d
,i
,o
,u
,x
, orX
conversion specifier applies to along
orunsigned long
argument; that a followingn
conversion specifier applies to a pointer to along
argument; that a followingc
conversion specifier applies to awint_t
argument; that a followings
conversion specifier applies to a pointer to awchar_t
argument; or has no effect on a followinga
,A
,e
,E
,f
,F
,g
, orG
conversion specifier.
I can also see that this is consistent with cppreference:fprintf where "%f"
and "%lf"
are equivalent for printf()
family.
So is this answer erroneous ? Or does C99
makes it clear now that "%f"
is for float
, while "%lf"
is for double
for printf()
family functions ?
Upvotes: 2
Views: 155
Reputation: 134396
TL;DR - The answer that you linked is perfectly fine. %f
in printf()
can handle double
and float
, both type of argument.
Information: printf()
is a variadic function.
As per C99
, chapter 7.19.6.3
#include <stdio.h>
int printf(const char * restrict format, ...);
and regarding the %f
format specifier,
f,F
A
double
argument representing a floating-point number is converted to decimal notation in the style [−]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. [....]
So, we see, the standard only mentions double
. Curious about its younger counterpart, float
?
Then, from chapter §6.5.2.2, paragraph 7
[...] The ellipsis notation in a function prototype declarator causes argument type conversion to stop after the last declared parameter. The default argument promotions are performed on trailing arguments.
and, regarding the default argument promotions part, (emphasis mine), paragraph 6,
[...] If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type
float
are promoted todouble
. These are called the default argument promotions. [...]
so, for printf()
function, with %f
format specifier, it does not matter whether the supplied argument is of type float
or double
, it will get promoted to double
anyway.
Upvotes: 2
Reputation: 727047
The documentation is correct: printf
sees no difference between float
and double
.
Parameters of functions from the printf
family are taken through the variable-length mechanism. In the variable-length part of a variable-length argument list, the "default argument promotions" apply: types char
and short int
are promoted to int
, and float
is promoted to double
.
Upvotes: 6