473,241 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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 1167
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.