C FAQ 15.2

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);
 
I

Ian Collins

arnuld said:
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().."
The first argument to printf is the format specifier, so printf always
has at least one argument.
 
A

arnuld

The first argument to printf is the format specifier, so printf always
has at least one argument.

you misunderstood my question. I asked *Why* FAQ has to use
sentence like "...variable length argument list..." when it is definitely
sure that printf always has variable length list.


BTW, regarding your answer that first argument to printf is aformat
specifier, I think, first argument to printf is always a string literal
(or string constant or string or character array or whatever you call it
in C) which may or may not *include* format specifiers.
 
I

Ian Collins

arnuld said:
you misunderstood my question. I asked *Why* FAQ has to use
sentence like "...variable length argument list..." when it is definitely
sure that printf always has variable length list.
Because there are two parts to the argument list, the fixed part and the
variable length part.
BTW, regarding your answer that first argument to printf is aformat
specifier, I think, first argument to printf is always a string literal
(or string constant or string or character array or whatever you call it
in C) which may or may not *include* format specifiers.
Either way, it still tells the function how many additional arguments
there are.
 
A

arnuld

Because there are two parts to the argument list, the fixed part and the
variable length part.

ok, show me:

printf("you entered %d\n", i);
printf("%d x %d = %d", i, j, result);

first argument: string literal
second argument: variables

string literals in both cases are of different lengths, or variable
lengths and so are the number of variables. Hence both arguments are of
variable length.
 
C

Chris Dollin

arnuld said:
BTW, regarding your answer that first argument to printf is aformat
specifier, I think, first argument to printf is always a string literal
(or string constant or string or character array or whatever you call it
in C)

We call it a "string". It doesn't need to be a literal, although it
usually is.
 
K

Keith Thompson

arnuld said:
ok, show me:

printf("you entered %d\n", i);

First let me make sure we're using the same terminology. A
"parameter" is an object declared as part of a function declaration or
definition. An "argument" is an expression passed to a function, one
of the one or more comma-separated expressions appearing between the
parentheses in a function call.

The above call has two arguments, one of type char* (resulting from
the implicit conversion of the string literal), and one that's either
of type int or of some type that promotes to int.
printf("%d x %d = %d", i, j, result);

This call has four arguments. The first, as above, is of type char*;
the other three are integers.
first argument: string literal
second argument: variables

string literals in both cases are of different lengths, or variable
lengths and so are the number of variables. Hence both arguments are of
variable length.

Quoting the FAQ again:

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.

Both occurrences of the phrase "variable-length" refer to the variable
number and type of arguments, not to the length of any one argument.

Most function prototypes (those without "...") specify exactly the
number and type of arguments that the function expects. Armed with
this information, the compiler is able in many cases to implicitly
convert arguments to the expected type. In a prototype that includes
"...", there must always be one or more parameters of specified type
preceding the "...". In a call to printf, the first argument matches
the "format" parameter, which is of type char*; no promotion takes
place. (There are no promotions for pointer arguments anyway.) The
remaining arguments, if any, correspond to the "...", which means that
there can be any number (the argument list is of variable length) and
type(s).

Consider a function declared like this:

void func(float a, float b, float c, ...);

and a call like this:

float f1, f2, f3, f4, f5, f6;
...
func(f1, f2, f3, f4, f5, f6);

The first three arguments correspond to the explicit parameters, so no
promotion occurs; they're passed as float values. The last three
arguments correspond to the "...", and so the default argument
promotions apply; they're all promoted to double. And within the body
of function, it must use ``va_arg(ap, double)'', not ``va_arg(ap,
float)'', to obtain the values of f4, f5, and f6.
 
S

saurabh

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().." does that mean in
this case of printf(), C language will not convert %c to an int:

Apart from all the answers,one more thing that comes to my mind is,
the question refers to the family of printf functions.e.g. printf
fprintf,sprintf .That is why it doesn't address printf specifically,
and says 'in the variable-length part of a variable-length argument
list'.

I think the OP has this confusion too.
 
A

arnuld

Apart from all the answers,one more thing that comes to my mind is,
the question refers to the family of printf functions.e.g. printf
fprintf,sprintf .That is why it doesn't address printf specifically,
and says 'in the variable-length part of a variable-length argument
list'.
I think the OP has this confusion too.


No read the FAQ again, 15.2 is only about printf(), there is no mention of
sprintf() or fprintf()
 
J

Jean-Marc Bourguet

arnuld said:
No read the FAQ again, 15.2 is only about printf(), there is no mention of
sprintf() or fprintf()

The first sentence states a rule which is valid for any function accepting
a variable length argument list.
 
J

James Kuyper

arnuld said:
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().."

The reason it says it that way is that the same exact rule applies to
any other function with a variable length argument list. It's important
to understand that this rule is not specific to printf().

Furthermore, you should realize that the first argument to printf() (and
the first two arguments to fprintf() and sprintf()) are fixed; they are
not in "the variable-length part ...". The default argument promotions
are applied only to the arguments after the fixed portion of the
argument list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,122
Latest member
VinayKumarNevatia_
Top