473,695 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable Argument Functions

i have statements
printf("%f",(do uble)1); /* works fine although i have sent %f for
double */
printf("%c",(in t)64); /* works fine although i have sent %c for
int */

Need your comment on this.

As my understanding, when we pass something on variable argument, its
type will be promoted . char is promoted to Int . float is promoted to
double . that is why above things work fine.

let me know your comments on above statements. we can extends this talk
for signed and unsigned , and long double.

Aug 30 '06 #1
5 2996
? "shaanxxx" <sh******@yahoo .com?????? ??? ??????
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
i have statements
printf("%f",(do uble)1); /* works fine although i have sent %f for
double */
printf("%c",(in t)64); /* works fine although i have sent %c for
int */

Need your comment on this.

As my understanding, when we pass something on variable argument, its
type will be promoted . char is promoted to Int . float is promoted to
double . that is why above things work fine.

let me know your comments on above statements. we can extends this talk
for signed and unsigned , and long double.
Actually nothing is promoted.
printf("%c",(in t)64)
works fine, because the first 8 bits of an int and a char are the same. But
that's not always the case.

The compiler doesn't have any way to know the types of the parameters
you pass on a function receiving a variable number of args, so no
promotion is possible.

--
Papastefanos Serafeim
Aug 30 '06 #2
Papastefanos Serafeim said:
? "shaanxxx" <sh******@yahoo .com?????? ??? ??????
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
>i have statements
printf("%f",(d ouble)1); /* works fine although i have sent %f for
double */
printf("%c",(i nt)64); /* works fine although i have sent %c for
int */

Need your comment on this.

As my understanding, when we pass something on variable argument, its
type will be promoted . char is promoted to Int . float is promoted to
double . that is why above things work fine.

let me know your comments on above statements. we can extends this talk
for signed and unsigned , and long double.

Actually nothing is promoted.
printf("%c",(in t)64)
works fine, because the first 8 bits of an int and a char are the same.
It does work fine, but that isn't the reason, as on some systems the first 8
bits of an int and a char are /not/ the same!

The reason it works fine is that 64 is already an int, so there is no
particular problem in converting it to one.

This works fine too:

char c = 'A'; /* 'A' is an int, but c is a char */
printf("%c\n", c);

c is a char, but the compiler will convert its value to int type before
passing that value to printf, which expects an int to match the %c
specifier.

As the Standard points out:

c The int argument is converted to an unsigned char , and the resulting
character is written.

Note: "The int argument"!

But that's not always the case.

The compiler doesn't have any way to know the types of the parameters
you pass on a function receiving a variable number of args, so no
promotion is possible.
[Nit: You pass arguments, not parameters, and the function receives
parameters, not arguments.]

The default argument promotions do in fact occur. See 4.8.1.2 in C89:

"The va_arg macro expands to an expression that has the type and
value of the next argument in the call. The parameter ap shall be the
same as the va_list ap initialized by va_start . Each invocation of
va_arg modifies ap so that the values of successive arguments are
returned in turn. The parameter type is a type name specified such
that the type of a pointer to an object that has the specified type
can be obtained simply by postfixing a * to type . If there is no
actual next argument, or if type is not compatible with the type of
the actual next argument (as promoted according to the default
argument promotions), the behavior is undefined."

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 30 '06 #3
Papastefanos Serafeim wrote:
? "shaanxxx" <sh******@yahoo .com?????? ??? ??????
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
i have statements
printf("%f",(do uble)1); /* works fine although i have sent %f for
double */
printf("%c",(in t)64); /* works fine although i have sent %c for
int */

Need your comment on this.

As my understanding, when we pass something on variable argument, its
type will be promoted . char is promoted to Int . float is promoted to
double . that is why above things work fine.
Correct. In fact, %f and %c are documented as accepting types double
and int, respectively, not float and (unsigned) char. It's only because
of the promotions that it will work with float and char. In fact,
ignoring the possibility of overflow, the below code is valid and
equivalent to printing "A\n":

printf("%c\n", 'A' + UCHAR_MAX + 1);
let me know your comments on above statements. we can extends this talk
for signed and unsigned , and long double.

Actually nothing is promoted.
printf("%c",(in t)64)
works fine, because the first 8 bits of an int and a char are the same. But
that's not always the case.
It will work fine on any conforming C implementation, as long as it
provides the standard I/O functions, and as long as 64 is a printable
character.
The compiler doesn't have any way to know the types of the parameters
you pass on a function receiving a variable number of args, so no
promotion is possible.
The logic is sane enough, but the conclusion should be a bit different:
promotion is done regardless of the format string.

Aug 30 '06 #4

Papastefanos Serafeim wrote:
? "shaanxxx" <sh******@yahoo .com?????? ??? ??????
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
i have statements
printf("%f",(do uble)1); /* works fine although i have sent %f for
double */
printf("%c",(in t)64); /* works fine although i have sent %c for
int */

Need your comment on this.

As my understanding, when we pass something on variable argument, its
type will be promoted . char is promoted to Int . float is promoted to
double . that is why above things work fine.

let me know your comments on above statements. we can extends this talk
for signed and unsigned , and long double.

Actually nothing is promoted.
printf("%c",(in t)64)
works fine, because the first 8 bits of an int and a char are the same. But
that's not always the case.

The compiler doesn't have any way to know the types of the parameters
you pass on a function receiving a variable number of args, so no
promotion is possible.

--
Papastefanos Serafeim
#include <stdarg.h>

/* minprintf: minimal printf with variable argument list */
void minprintf(char *fmt, ...)
{
va_list ap; /* points to each unnamed arg in turn */
char *p, *sval;
int ival;
double dval;

va_start(ap, fmt); /* make ap point to 1st unnamed arg */
for (p = fmt; *p; p++) {
if (*p != '%') {
putchar(*p);
continue;
}
switch (*++p) {
case 'd':
ival = va_arg(ap, int);
printf("%d", ival);
break;
case 'f':
dval = va_arg(ap, double);
printf("%f", dval);
break;
case 's':
for (sval = va_arg(ap, char *); *sval; sval++)
putchar(*sval);
break;
default:
putchar(*p);
break;
}
}
va_end(ap); /* clean up when done */
}

above code is taken from "The C programming Language"
By Brian W. Kernighan and Dennis M. Ritchie.

look at case float case, author have used double for the case of float.
If we change following code

case 'f':
dval = va_arg(ap, double);
printf("%f", dval);
break;
to

case 'f':
dval = va_arg(ap, float);
printf("%f", dval);
break;

It will not work for float since float is alway passed as double on
argument (stack : non standard word ).

I tested on windows compiler , it didnt work.

Aug 30 '06 #5
On Wed, 30 Aug 2006 10:12:51 +0000, Richard Heathfield
<in*****@invali d.invalidwrote:
Papastefanos Serafeim said:
<snip printf case, with correct response of %c taking int argument>
But that's not always the case.

The compiler doesn't have any way to know the types of the parameters
you pass on a function receiving a variable number of args, so no
promotion is possible.

[Nit: You pass arguments, not parameters, and the function receives
parameters, not arguments.]

The default argument promotions do in fact occur. See 4.8.1.2 in C89:
<snipped>
But that's only an aside, plus it's not clear that variadic std-lib
routines *printf and *scanf must actually use va_arg and friends.
(Plus that specific document is now rather difficult to get.)

The directly applicable statement is in C90 6.3.2.2 or C99 6.5.2.2p7:
"... The ellipsis notation in a function prototype declarator causes
argument type conversion to stop after the last declared parameter.
The default argument promotions are performed on trailing arguments."

I would add: since as far as the standard is concerned there is only
one implementation at a time and thus the same compiler is responsible
for both caller and callee sides, it actually _could_ implement a
scheme that identifies (and checks) types. But there is no standard
requirement to do so, nor portable way to access it; and thus
programmers habituated to its absence don't even ask for it, and
implementors seeing no demand and nontrivial cost don't provide it.
<evil smirkHey, Jacob, come over here a moment ... </>

- David.Thompson1 at worldnet.att.ne t
Sep 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6286
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments passed to the function are strings or must be (automaticly converted to a string e.g. the number 10 should become the string "10". My problem is that I can only find samples and description of printf() like functions where the optional arguments and...
134
7870
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
5
411
by: matevz bradac | last post by:
Hi, I'm trying to implement delayed function execution, similar to OpenGL's display lists. It looks something like this: beginList (); fcn1 (); fcn2 ();
1
2202
by: S?ren Gammelmark | last post by:
Hi I have been searching the web and comp.lang.c of a method of using variable-argument function pointers and the like. And some of the questions arising in this post are answered partly in these posts, but I ask mainly for a way to found a way to solve this problem. I'm a hobbyist so there might be an alltogether better solution to the problem, so please come with any solution you might think of. I'm open to ideas. --- And i'm not...
23
19188
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
4279
by: Tomás | last post by:
Let's say we have a variable length argument list function like so: unsigned GetAverage(unsigned a, unsigned b, ...); I wonder does the compiler go through the code looking for invocations of the function, and from there, treat it sort of like a template function? For instance, if we had a template function like so: template<class T> T GetAverage(T a, T b);
3
2512
by: prasanthag | last post by:
Hi, I am a newbie to this group. I have a problem in handling the variable arguments passed to a function. My requirement is like this. I have 2 functions say, void funcX(int i, int j); void funcY(int i, int j,char *name);
18
4054
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
6
3216
by: CptDondo | last post by:
How do you declare a function with 0 or mroe arguments? I have a bunch of functions like this: void tc_cm(int row, int col); void tc_do(void); void tc_DO(int ln); and I am trying to declare a pointer to them:
19
2128
by: Spiros Bousbouras | last post by:
Every time I've seen an example of a variable argument list function its functionality was to print formatted output. Does anyone have examples where the function is not some variation of printf ?
0
8649
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8586
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9004
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3025
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2289
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1986
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.