What is the signature of printf?

前端 未结 3 340
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 15:46

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?

相关标签:
3条回答
  • 2020-12-03 16:10

    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.

    0 讨论(0)
  • 2020-12-03 16:16
    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);
    
    0 讨论(0)
  • 2020-12-03 16:23

    Method signature, for some additional context.

    0 讨论(0)
提交回复
热议问题