News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

"C" Programming Language - Variable-Length Argument Lists

Started by sivaji, Jan 10, 2008, 06:50 PM

Previous topic - Next topic

sivaji

Programming Stuffs in "C" - Technical Skills for INTERVIEW

Variable-Length Argument Lists

1: How can I write a function that takes a variable number of arguments?

By declaring it with a variable number of arguments in the prototype. Use only the arguments declared at any given time.
to do most of the work?

Redefine printf; the call to ``printf'' inside yours will be resolved to the library version, because the C language doesn't allow recursion.

2: How can I discover how many arguments a function was actually called with?

_args is an external integer constant. It evaluates to three times the number of arguments the current function was called with. You can then look at _argdata[args] to get the address of the last arg, _argdata[args - 1] to get the size of the last arg, and _argdata[args - 2] to get the type of the last arg (as an int).

N.B. You *MUST* not refer to _args or _argdata between the ()'s of a function call; their value will be indeterminate. Use temporary storage.

3: Why doesn't
   printf("hello, ", "world!", '\n');
work? I thought printf() took a variable number of arguments.

It will probably work some of the time; the number of arguments used by printf() may vary, as it is a variadic function.
Am now @ Chennai