468,291 Members | 1,492 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,291 developers. It's quick & easy.

C FAQ 15.2

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

--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is Blocked. Reason: Excessive Spamming

Sep 18 '08 #1
10 992
arnuld wrote:
>
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.

--
Ian Collins.
Sep 18 '08 #2
On Thu, 18 Sep 2008 17:12:00 +1200, Ian Collins wrote:
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.


--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is Blocked. Reason: Excessive Spamming

Sep 18 '08 #3
arnuld wrote:
>On Thu, 18 Sep 2008 17:12:00 +1200, Ian Collins wrote:
>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.
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.

--
Ian Collins.
Sep 18 '08 #4
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);
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.


--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is Blocked. Reason: Excessive Spamming

Sep 18 '08 #5
arnuld wrote:
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.

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Sep 18 '08 #6
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"
Sep 18 '08 #7
On Thu, 18 Sep 2008 09:47:37 +0500, arnuld wrote:
>
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.
Sep 18 '08 #8
On Thu, 18 Sep 2008 13:01:47 +0500, saurabh wrote:
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()
--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is Blocked. Reason: Excessive Spamming

Sep 18 '08 #9
arnuld <su*****@invalid.addresswrites:
On Thu, 18 Sep 2008 13:01:47 +0500, saurabh wrote:
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()
The first sentence states a rule which is valid for any function accepting
a variable length argument list.

--
Jean-Marc
Sep 18 '08 #10
arnuld wrote:
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.
Sep 18 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by William C. White | last post: by
2 posts views Thread by Albert Ahtenberg | last post: by
3 posts views Thread by James | last post: by
reply views Thread by Ollivier Robert | last post: by
1 post views Thread by Richard Galli | last post: by
4 posts views Thread by Albert Ahtenberg | last post: by
1 post views Thread by inderjit S Gabrie | last post: by
2 posts views Thread by Jack | last post: by
3 posts views Thread by Sandwick | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.