int printf(const char *format, ...);
Why printf can receive more than 2 arguments although there is no overloading in c.
Anyone can give me some brief explanation the use of ...(3 dots) in printf function argument?
int printf(const char *format, ...);
Why printf can receive more than 2 arguments although there is no overloading in c.
Anyone can give me some brief explanation the use of ...(3 dots) in printf function argument?
... in the parameter list means that 0 or more arguments could be passed. You access these arguments with the stdarg.h functions.
Code:#include <stdio.h> #include <stdlib.h> #include <stdarg.h> void myprintf(const char* lpszText, ...) { va_list args; char szMessage[256]; va_start(args,lpszText); vsprintf(szMessage,lpszText,args); va_end(args); fputs(szMessage,stdout); } int main(void) { myprintf("The numbers are %d and %d\n",5,1); return EXIT_SUCCESS; }
lpszThisIsMyFormatString. i like that.
vice, you need to read a book.
"The C programming Language" by Kernighan & Ritchie,
chapter 7.3, "Variable-length Argument Lists"
thanks for the explanation