简体   繁体   中英

What is the signature of printf?

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?

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);

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.

Method signature , for some additional context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM