Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   variable arguments in C (http://www.go4expert.com/forums/variable-arguments-c-t5287/)

hitesh_mathpal 16Jul2007 14:48

variable arguments in C
 
how can a function take variable argument in C, as printf?

shabbir 16Jul2007 20:26

Re: variable arguments in C
 
Its very simple. You need to be using the va_ functions

Code: C

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  */

}



All times are GMT +5.5. The time now is 09:53.