Recently in an interview I was asked about what the signature of printf is. I really couldn't get a right answer. Would someone be able to shed some light on this?
Asked
Active
Viewed 1.1k times
9
-
2If you're stumped in an interview, especially on a question of fact, ask the interviewer! If you're polite, and they're not a jerk, I can't imagine them refusing you. – Ken Mar 11 '09 at 06:10
3 Answers
26
int printf ( const char * format, ... );
They were probably asking this to see if you were familiar with the optional parameter syntax "...". This allows you to pass an indeterminate list of variables that will fill in the format string.
For example, the same method can be used to print things like this:
printf("This is a string: %s", myString);
printf("This is a string: %s and an int: %d", myString, myInt);

Andy White
- 86,444
- 48
- 176
- 211
-
Or they might have been wondering if the interviewee knew printf() returns an int. – Mitch Haile Mar 11 '09 at 04:52
-
1
-
1I hope they were after the variatic syntax. If they wanted to know about the return type, the question was a terrible one. You don't want to work there. – Steve Rowe Mar 11 '09 at 05:48
-
Yeah I agree with that. I hate when you get an interviewer that just tries to trick you with some obscure thing that nobody ever uses – Andy White Mar 11 '09 at 05:50
-
-
Whoops... I guess I haven't been. I'll stick with Console.WriteLine or System.out.println - those return void :) – Andy White Mar 11 '09 at 06:16
-
1That's the C89 version: in C99, you need a restrict in there, too: int printf(const char * restrict format, ...); – Jonathan Leffler Mar 11 '09 at 06:36
-
Ha ha, I've never heard of "restrict." I think I'm content with not knowing what that is. – Andy White Mar 11 '09 at 06:41
-
@Andy: in most cases, you can get away with that; in some cases, the compiler might produce sub-optimal code, though – Christoph Mar 11 '09 at 10:04
-
when asking about scanf, i suspect return value questions become important – Johannes Schaub - litb Mar 11 '09 at 15:01
-
-
-
1@Andy: see eg http://www.cellperformance.com/mike_acton/2006/05/demystifying_the_restrict_keyw.html (first hit on google for `gcc restrict`) – Christoph Mar 11 '09 at 17:45
-
If I ever write C code again, I'll check it out, but for now, ignorance is bliss. – Andy White Mar 11 '09 at 18:20
-
When using variable argument lists (`...`), isn't it required that the previous argument is an `int` giving the length of the variable argument list? How does `printf` get away with skipping this part? – qntm Feb 02 '17 at 00:23
8
printf is a variadic function with the following signature:
int printf(const char *format, ...);
this means that it has one required string parameter, followed by 0 or more parameters (which can be of various types). Finally, it returns an int which represents how many characters are in the result.
The number and type of the optional parameters is determined by the contents of the format string.

Evan Teran
- 87,561
- 32
- 179
- 238