C programming vsprintf stdio.h


int vsprintf(char * restrict s ,
 const char * restrict format,
 va_list arg);

Parameters:
s -This array stores the outputted data.

format -The data with the conversion specifier that is to be written to ‘s’.

arg -A va_arg macro that consists of the variable names.

Return type
int -Returns the number of characters written in the array,not counting the terminating null character,or a negative value if an encoding error occurred.

In C programming the <stdio.h> vsprintf function is equivalent to sprintf function but instead of using ellipsis as the third argument it is replaced by va_list macro (found in <stdarg.h> header ).A code example is given below.

Link : C sprintf stdio.h

Code example

void func(char* format , …)
{
int ret;

va_list arg ;

va_start(arg , format);

ret=vsprintf(format , arg);

printf(“\nret=%d” , ret);

va_end( arg );
}

int main( )
{
char s[]=”New name”;

float f=12.3456 ;

int val=45 ;

func( “%s %.3f %d” , str , f ,val);

printf(“\nstr=%s” , str);

getchar( );
return 0 ;
}

Output,

ret=18
s=’New name 12.345 13′

‘ret’ gives the number of characters written to ‘s’ string.


Related links

->C vsnprintf

->C fprintf

->C vprintf

->C snprintf