Functions with variable arguments

S

Sathyaish

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?
 
C

CBFalconer

Sathyaish said:
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.
 
S

SM Ryan

# 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);
}
 
J

Jaspreet

Sathyaish said:
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.
 
S

Suman

Jaspreet said:
Sathyaish said:
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.
 
J

Jaspreet

Suman said:
Jaspreet said:
Sathyaish said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top