Syntax
#include <stdio.h> int sprintf(char *buffer, const char *format-string, argument-list);Description
sprintf formats and stores a series of characters and values in the array buffer. Any argument-list is converted and put out according to the corresponding format specification in the format-string.
The format-string consists of ordinary characters and has the same form and function as the format-string argument for the printf function. See printf for a description of the format-string and arguments.
In extended mode, sprintf also converts floating-point values of NaN and infinity to the strings "NAN" or "nan" and "INFINITY" or "infinity". The case and sign of the string is determined by the format specifiers. See Infinity and NaN Support for more information on infinity and NaN values.
sprintf returns the number of bytes written in the array, not counting the ending null character.
This example uses sprintf to format and print various data.
#include <stdio.h> char buffer[200]; int i,j; double fp; char *s = "baltimore"; char c; int main(void) { c = 'l'; i = 35; fp = 1.7320508; /* Format and print various data */ j = sprintf(buffer, "%s\n", s); j += sprintf(buffer+j, "%c\n", c); j += sprintf(buffer+j, "%d\n", i); j += sprintf(buffer+j, "%f\n", fp); printf("string:\n%s\ncharacter count = %d\n", buffer, j); return 0; /**************************************************************************** The output should be: string: baltimore l 35 1.732051 character count = 24 ****************************************************************************/ }Related Information