printf function arguments

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tgiri
    New Member
    • Apr 2007
    • 1

    printf function arguments

    hello, this is gireesh.
    can u pls give me solution to this question.
    PLATFORM : C language
    Q. What are the input arguments passed to the printf() function?

    actually i found in header file i.e;
    extern int printf(const *char, ....);
    what is the meaning of .... in printf function?

    pls give me reply as early as possible.
    my email id : <snipped>
    Last edited by Banfa; Apr 27 '07, 12:10 PM. Reason: Email address removed, please see site FAQ
  • svlsr2000
    Recognized Expert New Member
    • Feb 2007
    • 181

    #2
    Originally posted by tgiri
    hello, this is gireesh.
    can u pls give me solution to this question.
    PLATFORM : C language
    Q. What are the input arguments passed to the printf() function?

    actually i found in header file i.e;
    extern int printf(const *char, ....);
    what is the meaning of .... in printf function?

    pls give me reply as early as possible.
    my email id : tgiree@gmail.co m
    ... is used to get variable length arguments. these arguments are retrieved using some macros and functions declared in stdarg.h
    You can have your own print like this
    Code:
    void
    miniprintf(const char *fmt, ...)
    {
    	const char *p;
    	int i;
    	unsigned u;
    	char *s;
    	va_list argp;
    
    	va_start(argp, fmt);
    
    	for(p = fmt; *p != '\0'; p++) {
    		if(*p != '%') {
    			putchar(*p);
    			continue;
    		}
    
    		switch(*++p) {
    		case 'c':
    			i = va_arg(argp, int);
    			/* *not* va_arg(argp, char); see Q 15.10 */
    			putchar(i);
    			break;
    
    		case 'd':
    			i = va_arg(argp, int);
    			if(i < 0) {
    				/* XXX won't handle INT_MIN */
    				i = -i;
    				putchar('-');
    			}
    			fputs(baseconv(i, 10), stdout);
    			break;
    
    		case 'o':
    			u = va_arg(argp, unsigned int);
    			fputs(baseconv(u, 8), stdout);
    			break;
    
    		case 's':
    			s = va_arg(argp, char *);
    			fputs(s, stdout);
    			break;
    
    		case 'u':
    			u = va_arg(argp, unsigned int);
    			fputs(baseconv(u, 10), stdout);
    			break;
    
    		case 'x':
    			u = va_arg(argp, unsigned int);
    			fputs(baseconv(u, 16), stdout);
    			break;
    
    		case '%':
    			putchar('%');
    			break;
    		}
    	}
    
    	va_end(argp);
    }
    Note : This code has been copied from c faq

    ... is known as ellipsis operator

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by svlsr2000
      ... is used to get variable length arguments.
      For clarification the arguments are not variable length (it is not possible for arguments to be of variable length). It is the number of arguments that is variable.

      ... is used to indicated that an unspecified number of extra arguments can be present in the function call.

      Functions using the elipsis to indicate a variable number of function arguments are called varidac functions.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Make that 'variadic' ;-)

        kind regards,

        Jos

        Comment

        Working...