You may be looking for snprintf .
int snprintf(char *str, size_t size, const char *format, ...);
A simple example:
char buffer[100];
int value = 42;
int nchars = snprintf(buffer, 100, "The answer is %d", value);
printf("%s\n", buffer);
GNU also has an example .
Just to add, you actually don't need to use it snprintf- you can use the plain old one sprintf(without the size argument), but then it's harder to guarantee that only n characters are written to the buffer, GNU also has a nice function asprintf, which will allocate a buffer for you.
source
share