Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
3  
It is known as a variadic function. –  MichaelT Mar 9 at 14:50
1  
Related. –  Blrfl Mar 9 at 17:19

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer
    
There is a small example on the va_start man page. –  ott-- Mar 9 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. –  Bart van Ingen Schenau Mar 9 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 at 0:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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