printf function explanation

This is a discussion on printf function explanation within the C Programming forums, part of the General Programming Boards category; int printf(const char *format, ...); Why printf can receive more than 2 arguments although there is no overloading in c. ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    15

    printf function explanation

    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?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    ... 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;
    }

  3. #3
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    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"

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    thanks for the explanation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21