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.

look at these calls closely:

printf("hello, world\n");
printf("%d", 2);
printf("%d%g\n", 2, 2.3);

we see that printf can accept any type and any number of args. however we know that functions in c only take fixed length args and should have a compatible prototype to match the arg. what would be the prototype of printf ?

share|improve this question
1  
if you don't know variadic functions, this question cannot be answered ! –  Abhinav Pandey Nov 29 '11 at 12:01
7  
and if you already know variadic functions, why on earth did you post this question? :-/ –  Péter Török Nov 29 '11 at 15:15

2 Answers 2

we know that functions in c only take fixed length args

So you know it incorrectly. printf (and several other standard library functions) use a language feature called variable argument list.

Btw its signature declaration looks more or less like this (may be different on your platform):

int printf ( const char * format, ... );
share|improve this answer
    
whether you write the "format" or not doesn't matter in function declaration –  Abhinav Pandey Nov 29 '11 at 12:34
2  
@AbhinavPandey, at least if readability (by humans) doesn't matter, that is :-) –  Péter Török Nov 29 '11 at 12:38
    
"function signature" really is a C++ term associated with overloading; C doesn't do overloading so it only has a function declaration. –  MSalters Nov 29 '11 at 13:08
    
Wouldn't ommiting format in the declaration make it possible to call printf(1); without even a compiler warning? –  user281377 Nov 29 '11 at 13:18
    
@MSalters, thanks, fixed. –  Péter Török Nov 29 '11 at 15:14
extern int printf (__const char *__restrict __format, ...);

The ellipsis indicate that this function uses a variable argument list.

share|improve this answer
    
Why the leading underscores for const and restrict? Those are perfectly good keywords in C99, and the right ones for the function declaration. (__format is a reserved identifier, but may be correct for a particular implementation.) –  David Thornley Nov 29 '11 at 15:21
2  
Because the declaration is copied-and-pasted from the GNU C Library version of stdio.h. –  dan04 Nov 29 '11 at 15:45
    
Dan04: exactly. Lazy me ;-) –  user281377 Nov 29 '11 at 22:41

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.