1. 函数原型
int sprintf(
char *buffer,
const char *format [,
argument] ...
);
2. 参数说明
buffer
Storage location for output
format
Format-control string
argument
Optional arguments
Return Value
The number of characters written, or –1 if an error occurred. If buffer or format is a null pointer, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions return -1 and set errno to EINVAL.
3. 应用举例
#include <stdio.h>
int main( void )
{
char buffer[200], s[] = "computer", c = 'l';
int i = 35, j;
float fp = 1.7320534f;
// Format and print various data:
j = sprintf( buffer, " String: %s\n", s ); // C4996
j += sprintf( buffer + j, " Character: %c\n", c ); // C4996
j += sprintf( buffer + j, " Integer: %d\n", i ); // C4996
j += sprintf( buffer + j, " Real: %f\n", fp );// C4996
// Note: sprintf is deprecated; consider using sprintf_s instead
printf( "Output:\n%s\ncharacter count = %d\n", buffer, j );
}
4. 执行结果
Output:
String: computer
Character: l
Integer: 35
Real: 1.732053
character count = 79