sprintf_s,计算机编程语言函数,其功能是将数据格式化输出到字符串。
sprintf_sprintf_s -函数简介
函数功能:将数据格式化输出到字符串
函数原型:
intsprintf_s(
char *buffer,
size_tsizeOfBuffer,
constchar *format [,
argument] ...
);
头文件:stdio.h
相关函数:
int sprintf( char *buffer, const char *format [, argument] … );
int vsprintf( char *buffer, const char *format, va_list argptr );
int _sprintf_s_l(
char *buffer,
size_t sizeOfBuffer,
const char *format,
locale_t locale [,
argument] ...
);
int swprintf_s(
wchar_t*buffer,
size_t sizeOfBuffer,
const wchar_t *format [,
argument]...
);
int _swprintf_s_l(
wchar_t *buffer,
size_t sizeOfBuffer,
const wchar_t *format,
locale_t locale [,
argument]…
);
template
int sprintf_s(
char (&buffer)[size],
const char *format [,
argument] ...
); // C++ only
template
int swprintf_s(
wchar_t (&buffer)[size],
const wchar_t *format [,
argument]...
); // C++ only
备注:
sprintf_s()是sprintf()的安全版本,通过指定缓冲区长度来避免sprintf()存在的溢出风险
sprintf_sprintf_s -程序示例
// crt_sprintf_s.c
// This program uses sprintf_s to format various
// data and place them in the string named buffer.
//
#include
int main( void )
{
char buffer, s[] = "computer", c = 'l';
int i = 35, j;
float fp = 1.7320534f;
// Format and print various data:
j = sprintf_s( buffer, 200, " String: %sn", s );
j += sprintf_s( buffer + j, 200 - j, " Character: %cn", c );
j += sprintf_s( buffer + j, 200 - j, " Integer: %dn", i );
j += sprintf_s( buffer + j, 200 - j, " Real: %fn", fp );
printf_s( "Output:n%sncharacter count = %dn", buffer, j );
}
Output:
String: computer
Character: l
Integer: 35
Real: 1.732053
character count = 79