471,082 Members | 708 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Functions with variable arguments

I knew this but I have forgotten. How do you declare a function that
has to accept a variable number of arguments?

For instance, the printf() function has a variable number of arguments
it can take.

I believe languages such as Visual Basic call this feature Param
Array().

I believe the syntax had something to do with suffixing three periods
in the argument prototype, but I can't be sure. Can someone shed light
as to the syntax and also the restrictions or limitations that come
with having variable arguments in functions? Do they have to be the
last argument? It seems quite logical to think that "they" need to be
the last argument in the function definition. Are there any other
nuances to having variable arguments?

Nov 15 '05 #1
5 1617
Sathyaish wrote:

I knew this but I have forgotten. How do you declare a function
that has to accept a variable number of arguments?


You pull out your elementary C book and read it.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 15 '05 #2
"Sathyaish" <Sa*******@Yahoo.com> wrote:
# I knew this but I have forgotten. How do you declare a function that
# has to accept a variable number of arguments?
#
# For instance, the printf() function has a variable number of arguments
# it can take.

int xprintf(char *format,...) {
va_list argv;
va_start(argv,format);
.....
va_arg(argv,Type);
.....
va_end(argv);
}

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I think that's kinda of personal; I don't think I should answer that.
Nov 15 '05 #3
Sathyaish wrote:
I knew this but I have forgotten. How do you declare a function that
has to accept a variable number of arguments?

[snip]

You use the ellipses (...) to decl. a function that accepts variable
number of arguments.

It could be:

void myVarArgs(char *fmt, ...);

and then in the defintion you could va_args, va_start library calls.

Nov 15 '05 #4


Jaspreet wrote:
Sathyaish wrote:
I knew this but I have forgotten. How do you declare a function that
has to accept a variable number of arguments?
[snip] and then in the defintion you could va_args, va_start library calls.

1. No such thing as va_args.
2. va_arg, va_end, and va_start are macros, not library calls
Regards,
Suman.

Nov 15 '05 #5
Suman wrote:
Jaspreet wrote:
Sathyaish wrote:
I knew this but I have forgotten. How do you declare a function that
has to accept a variable number of arguments?

[snip]
and then in the defintion you could va_args, va_start library calls.

1. No such thing as va_args.
2. va_arg, va_end, and va_start are macros, not library calls
Regards,
Suman.


Thanks for correcting me. Apologies for the typos.

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

99 posts views Thread by David MacQuigg | last post: by
9 posts views Thread by Gibby Koldenhof | last post: by
6 posts views Thread by Fuzzyman | last post: by
6 posts views Thread by CptDondo | last post: by
reply views Thread by leo001 | last post: by

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.