Format
#include <stdio.h> int sprintf(char *buffer, const char *format, argument-list);
Language Level: ANSI, POSIX, XPG4, Extension
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. For a description of the format string and arguments, see printf -- Print Formatted Characters.
In extended mode, on both OS/2 and Windows, 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.
If you specify a null string, on OS/2 and Windows, for the %s or %ls format specifier, sprintf prints (null). (In previous releases of the C/C++ run-time library, sprintf produced no output for a null string.)
Return Value
sprintf returns the number of bytes written in the
array, not counting the ending null character.
Example
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
1
35
1.732051
character count = 24 *************************************************************/ }
![]()
_cprintf -- Print Characters to Screen
fprintf -- Write Fomatted Data to a
Stream
printf -- Print Formatted Characters
sscanf -- Read Data from Buffer
swprintf -- Format and Write Wide
Characters to Buffer
vfprintf -- Print Argument Data to
Stream
vprintf -- Print Argument Data
vsprintf -- Print Argument Data to
Buffer
vswprintf -- Format and Write Wide
Characters to Buffer
<stdio.h>