Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the signature of printf?

Tags:

c

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?

like image 363
Tommy Avatar asked Mar 11 '09 04:03

Tommy


People also ask

What is printf () in C?

The printf() function sends a formatted string to the standard output (the display). This string can display formatted variables and special control characters, such as new lines ('\n'), backspaces ('\b') and tabspaces ('\t'); these are listed in Table 2.1.

How is printf function defined?

View More. C++ printf is a formatting function that is used to print a string to stdout. The basic idea to call printf in C++ is to provide a string of characters that need to be printed as it is in the program. The printf in C++ also contains a format specifier that is replaced by the actual value during execution.


2 Answers

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);
like image 118
Andy White Avatar answered Sep 18 '22 20:09

Andy White


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.

like image 21
Evan Teran Avatar answered Sep 20 '22 20:09

Evan Teran