Thread View: comp.unix.programmer
8 messages
8 total messages
Started by Kenny McCormack
Wed, 11 Oct 2000 00:00
Variable length arg lists in C (gcc): stdarg vs. varargs
Author: Kenny McCormack
Date: Wed, 11 Oct 2000 00:00
Date: Wed, 11 Oct 2000 00:00
39 lines
2067 bytes
2067 bytes
Notes, presented in no particular order:
1) I don't know if this is a GCC under Unix specific question (which
is my primary/only focus) or if it relates to all C compilers
and/or all platforms.
2) From what I can tell, there is a thing called varargs that is:
a) More powerful than stdarg
b) No longer documented
c) No longer supported
d) Not (as) portable
3) Here's the code that I have (I wrote it and it works fine):
/* Find the max of a list of ints, 0 marks the end of the list */
#include <stdarg.h>
int max(int n, ...)
{
va_list ap;
int maxnum = n;
va_start(ap, n);
while ((n = va_arg(ap, int)))
if (n > maxnum)
maxnum = n;
va_end(ap);
return maxnum;
}
4) Question: From what I can tell, stdarg has the following
limitations:
a) No way to tell how many args there are. I.e., you have
to have some kind of marker for the end of the list. If
you try to access more args than are passed, strange and
bad things will happen.
b) You have to have at least one fixed arg. This causes it
to fail the "acid test" - namely, "Can I write a wrapper
around printf?" This is mentioned in the man page
("man stdarg" on a Linux system).
So, have I got this all right? In particular, I am curious about the
questions raised in #4, and wondering if it is possible to use varargs
(which I can tell exist, from the header files, but I can't find docu on)
Re: Variable length arg lists in C (gcc): stdarg vs. varargs
Author: Kenny McCormack
Date: Wed, 11 Oct 2000 00:00
Date: Wed, 11 Oct 2000 00:00
47 lines
2934 bytes
2934 bytes
In article <xm2F5.47$xe3.691@burlma1-snr2>, Barry Margolin <barmar@genuity.net> wrote: ... >Basically, K&R C had varargs. The standardization committee designed >stdargs as the replacement for it. The main difference has to do with the >fact that stdargs is designed to coexist with function prototypes, i.e. the >funcname(<fixed args>,...) notation. I can't quite parse your meaning here. Are you saying that varargs did not have the "at least one fixed arg" requirement? If so, then that's what I meant in the original post when I stated that it was my understanding that varargs was more powerful (I.e., more powerful b/c it did not have this requirement/limitation) >>What I'd like to see is an implementation of the max() function that doesn't >>require a 0 to be passed to signal the end of the list (and, consequently, >>has the limitation that 0 can't be a valid data point). > >Neither varargs nor stdargs provides a way to find out how many arguments >were passed. If you don't want to pass a marker for the end of the list, >pass the number of arguments as the first argument, e.g. > >max(5, 100, 0, -10, 150, 9) > >This also solves the problem of requiring at least one fixed argument for >this function. OK. Point taken. >>But thanks for the note about vprintf - this seems to imply that there >>should be wrapper functions provided by the system for any "standard" >>functions that take variable numbers of args. I note that there is a >>vscanf; are there any other standard functions that take variable numbers of >>args? (I can't think of any off hand, but I haven't tried very hard...) > >Are you asking about functions that take variable number of arguments, or >functions like vprintf, that take an explicit va_list argument. Functions like printf and scanf that take a variable number of args. My point, made also by another poster, is that a well designed "function that takes variable numbers of args" will also come in the form of a companion function that takes a va_list arg. >I don't think there's a vscanf(), though (it's not on my Solaris 2.6 >system). Present on Linux, but not on Solaris (AFAICT).
Re: Variable length arg lists in C (gcc): stdarg vs. varargs
Author: Kenneth Chiu
Date: Wed, 11 Oct 2000 00:00
Date: Wed, 11 Oct 2000 00:00
14 lines
1129 bytes
1129 bytes
In article <8s26b3$9vl$1@yin.interaccess.com>,
Kenny McCormack <gazelle@interaccess.com> wrote:
> 4) Question: From what I can tell, stdarg has the following
> limitations:
>...
> b) You have to have at least one fixed arg. This causes it
> to fail the "acid test" - namely, "Can I write a wrapper
> around printf?" This is mentioned in the man page
> ("man stdarg" on a Linux system).
>
If your goal is to write a wrapper for printf, you can use stdarg to
write a wrapper around the vprintf() series. This may or may not be
good enough for you.
Re: Variable length arg lists in C (gcc): stdarg vs. varargs
Author: Kenny McCormack
Date: Wed, 11 Oct 2000 00:00
Date: Wed, 11 Oct 2000 00:00
30 lines
2178 bytes
2178 bytes
In article <8s2a3q$ur8$1@flotsam.uits.indiana.edu>,
Kenneth Chiu <chiuk@cs.indiana.edu> wrote:
>In article <8s26b3$9vl$1@yin.interaccess.com>,
>Kenny McCormack <gazelle@interaccess.com> wrote:
>> 4) Question: From what I can tell, stdarg has the following
>> limitations:
>>...
>> b) You have to have at least one fixed arg. This causes it
>> to fail the "acid test" - namely, "Can I write a wrapper
>> around printf?" This is mentioned in the man page
>> ("man stdarg" on a Linux system).
>>
>
>If your goal is to write a wrapper for printf, you can use stdarg to
>write a wrapper around the vprintf() series. This may or may not be
>good enough for you.
The goal is not to solve any particular problem (other than the "find the
maximum of a list of numbers" problem shown above), but rather to understand
how the various implementations (of the variable args functionality) work.
What I'd like to see is an implementation of the max() function that doesn't
require a 0 to be passed to signal the end of the list (and, consequently,
has the limitation that 0 can't be a valid data point).
But thanks for the note about vprintf - this seems to imply that there
should be wrapper functions provided by the system for any "standard"
functions that take variable numbers of args. I note that there is a
vscanf; are there any other standard functions that take variable numbers of
args? (I can't think of any off hand, but I haven't tried very hard...)
Re: Variable length arg lists in C (gcc): stdarg vs. varargs
Author: Barry Margolin
Date: Wed, 11 Oct 2000 00:00
Date: Wed, 11 Oct 2000 00:00
41 lines
2968 bytes
2968 bytes
In article <8s2apd$aqe$1@yin.interaccess.com>, Kenny McCormack <gazelle@interaccess.com> wrote: >The goal is not to solve any particular problem (other than the "find the >maximum of a list of numbers" problem shown above), but rather to understand >how the various implementations (of the variable args functionality) work. Basically, K&R C had varargs. The standardization committee designed stdargs as the replacement for it. The main difference has to do with the fact that stdargs is designed to coexist with function prototypes, i.e. the funcname(<fixed args>,...) notation. >What I'd like to see is an implementation of the max() function that doesn't >require a 0 to be passed to signal the end of the list (and, consequently, >has the limitation that 0 can't be a valid data point). Neither varargs nor stdargs provides a way to find out how many arguments were passed. If you don't want to pass a marker for the end of the list, pass the number of arguments as the first argument, e.g. max(5, 100, 0, -10, 150, 9) This also solves the problem of requiring at least one fixed argument for this function. >But thanks for the note about vprintf - this seems to imply that there >should be wrapper functions provided by the system for any "standard" >functions that take variable numbers of args. I note that there is a >vscanf; are there any other standard functions that take variable numbers of >args? (I can't think of any off hand, but I haven't tried very hard...) Are you asking about functions that take variable number of arguments, or functions like vprintf, that take an explicit va_list argument. I don't think there's a vscanf(), though (it's not on my Solaris 2.6 system). -- Barry Margolin, barmar@genuity.net Genuity, Burlington, MA *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups. Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
Re: Variable length arg lists in C (gcc): stdarg vs. varargs
Author: Kaz Kylheku
Date: Wed, 11 Oct 2000 00:00
Date: Wed, 11 Oct 2000 00:00
10 lines
1270 bytes
1270 bytes
On 11 Oct 2000 12:00:19 -0500, Kenny McCormack <gazelle@yin.interaccess.com> wrote: >Notes, presented in no particular order: > 1) I don't know if this is a GCC under Unix specific question (which > is my primary/only focus) or if it relates to all C compilers > and/or all platforms. > 2) From what I can tell, there is a thing called varargs that is: > a) More powerful than stdarg There is a language called assembly that is more powerful than C. ;)
Re: Variable length arg lists in C (gcc): stdarg vs. varargs
Author: Barry Margolin
Date: Thu, 12 Oct 2000 00:00
Date: Thu, 12 Oct 2000 00:00
36 lines
2704 bytes
2704 bytes
In article <8s2tvu$duk$1@yin.interaccess.com>, Kenny McCormack <gazelle@interaccess.com> wrote: >In article <xm2F5.47$xe3.691@burlma1-snr2>, >Barry Margolin <barmar@genuity.net> wrote: >... >>Basically, K&R C had varargs. The standardization committee designed >>stdargs as the replacement for it. The main difference has to do with the >>fact that stdargs is designed to coexist with function prototypes, i.e. the >>funcname(<fixed args>,...) notation. > >I can't quite parse your meaning here. Are you saying that varargs did not >have the "at least one fixed arg" requirement? If so, then that's what I >meant in the original post when I stated that it was my understanding that >varargs was more powerful (I.e., more powerful b/c it did not have this >requirement/limitation) Yes, varargs didn't have that requirement. However, since it didn't automatically pass the number of arguments, you still had to have at least one argument, either an initial argument that was used to determine how many variable arguments there are (like the format string in printf, or the count I suggested for your max() function) or a last argument with a distinguished value. >>I don't think there's a vscanf(), though (it's not on my Solaris 2.6 >>system). > >Present on Linux, but not on Solaris (AFAICT). So it's probably not required by ISO C or POSIX (unless it's a recent addition that hasn't made it into Solaris yet). -- Barry Margolin, barmar@genuity.net Genuity, Burlington, MA *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups. Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
Re: Variable length arg lists in C (gcc): stdarg vs. varargs
Author: Luis Oliveira
Date: Sun, 22 Oct 2000 00:00
Date: Sun, 22 Oct 2000 00:00
19 lines
1581 bytes
1581 bytes
Barry Margolin <barmar@genuity.net> wrote: >>>I don't think there's a vscanf(), though (it's not on my Solaris 2.6 >>>system). >> >>Present on Linux, but not on Solaris (AFAICT). > So it's probably not required by ISO C or POSIX (unless it's a recent > addition that hasn't made it into Solaris yet). vscanf is required by ISO C99 (section 7.19.6.11) but it wasn't required in the previous standard (C89). Linux's vscanf is probably a GNU extension. Cya @551, Lu�s Kernel 2.2.16, up 10 days [GNU/Linux User #173658]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads