The following C function:
int sprintf ( char * str, const char * format, ... );
int main ()
char buffer ;
int n, a=5, b=3;
n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
printf ("[%s] is a %d char long string\n",buffer,n);
What you want is one of these two functions:
snprintf (http://libslack.org/manpages/snprintf.3.html). It takes the length of the output buffer as its second argument, and if the buffer is too small for the result it will return the number of characters needed, allowing you to reallocate a larger buffer.
asprintf. It takes a
char ** argument and allocates enough memory to hold the output, as long as that much contiguous virtual memory is available. You have to call
free to remove it from memory if you're done with it before the program exits and may need the memory for something else.