0

I've always wondered where a single function such as printf(); can take in variable parameters and provide the right results. For example,

printf("Number is %d",a);

which has two parameters, and

printf("Numbers are %d and %d",a,b);

which has three variables. So, is there any possibilities for creating a user-defined function like these functions in C? If so, how can i do it? Thanks in advance.

2

1 Answer 1

5

Interestingly, the C standard defines what variadic function declarations should look like, but no guidance in how to implement them.

The mechanics for accessing arguments to variadic functions are therefore implementation-dependent. GNU C provides this via stdarg.h.

3
  • There is a small example on the va_start man page.
    – ott--
    Mar 9, 2015 at 15:44
  • 5
    Actually, the macros in stdarg.h are the standard way for accessing arguments in variadic functions. The thing left open by the standard is how those macros should be implemented. Mar 9, 2015 at 16:57
  • (and the macros are guaranteed to be provided by a compliant compiler, so you don't need to worry about it - writing and using variadic functions is perfectly well-defined)
    – Thomas
    Mar 10, 2015 at 0:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.