variable arguments in C

hitesh_mathpal's Avatar
Newbie Member
how can a function take variable argument in C, as printf?
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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  */

}