A
arnuld
Q: How can %f be used for both float and double arguments in
printf?Aren't they different types?
A: 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. (These are the same promotions that apply to
function calls without a prototype in scope, also known as
``old style'' function calls; see question 11.3.)
Therefore,printf's %f format always sees a double.
(Similarly, %c always sees an int, as does %hd.) See also
questions 12.9 and 12.13.
Look at the first sentence of the Answer: "In the variable-length part of
a variable-length argument list..."
I have little trouble understanding it. What exactly is the meaning of
this first sentence. I mean why this sentence was included when it is
pretty much clear that arguments to printf are *always* variable-length.
FAQ could said simply "the arguments to printf().."
or does that mean in this case of printf(), C language will not convert %c
to an int:
printf("You entered: %c\n", ch);
printf?Aren't they different types?
A: 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. (These are the same promotions that apply to
function calls without a prototype in scope, also known as
``old style'' function calls; see question 11.3.)
Therefore,printf's %f format always sees a double.
(Similarly, %c always sees an int, as does %hd.) See also
questions 12.9 and 12.13.
Look at the first sentence of the Answer: "In the variable-length part of
a variable-length argument list..."
I have little trouble understanding it. What exactly is the meaning of
this first sentence. I mean why this sentence was included when it is
pretty much clear that arguments to printf are *always* variable-length.
FAQ could said simply "the arguments to printf().."
or does that mean in this case of printf(), C language will not convert %c
to an int:
printf("You entered: %c\n", ch);