variable arguments in C

Discussion in 'C' started by hitesh_mathpal, Jul 16, 2007.

  1. hitesh_mathpal

    hitesh_mathpal New Member

    Joined:
    Jul 16, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    how can a function take variable argument in C, as printf?
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Its very simple. You need to be using the va_ functions

    Code:
    int my_printf( char *fmt, ... )
    {
      va_list  argptr;			/* Argument list pointer	*/
      char str[140];			/* Buffer to build sting into	*/
      int cnt;				/* Result of SPRINTF for return */
    
      va_start( argptr, format );		/* Initialize va_ functions	*/
      cnt = vsprintf( str, fmt, argptr );	/* prints string to buffer	*/
    
      va_end( argptr );			/* Close va_ functions		*/
      return( cnt );			/* Return the conversion count	*/
    
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice