9

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?

Steve Rowe
  • 19,411
  • 9
  • 51
  • 82
Tommy
  • 865
  • 4
  • 11
  • 17
  • 2
    If 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 Answers3

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
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
3

Method signature, for some additional context.

Bryan
  • 3,453
  • 6
  • 26
  • 20