Format
#include <stdio.h>
int sprintf(char *buffer, const char *format-string, argument-list);
Language Level: ANSI
Threadsafe: Yes.
Locale Sensitive: The behavior of this function might be affected by the LC_CTYPE and LC_NUMERIC categories of the current locale. The behavior might also be affected by the LC_UNI_CTYPE category of the current locale if LOCALETYPE(*LOCALEUCS2) or LOCALETYPE(*LOCALEUTF) is specified on the compilation command. For more information, see Understanding CCSIDs and Locales.
Description
The sprintf() function 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.
Return Value
The sprintf() function returns the number of bytes that are written in the array, not counting the ending null character.
Example that uses sprintf()
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);
}
/********************* Output should be similar to: *************
string:
baltimore
l
35
1.732051
character count = 24 */
Related Information