arnuld <su*****@invalid.addresswrites:
>On Thu, 18 Sep 2008 18:41:12 +1200, Ian Collins wrote:
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);
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.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"