sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_lsprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l

将设置格式的数据写入字符串。Write formatted data to a string. 可提供某些函数的更多安全版本,请参阅 sprintf_s、_sprintf_s_l、swprintf_s 和 _swprintf_s_lMore secure versions of some of these functions are available; see sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l. Swprintf_swprintf_l的安全版本不采用count参数。The secure versions of swprintf and _swprintf_l do not take a count parameter.

语法Syntax

int sprintf(
   char *buffer,
   const char *format [,
   argument] ...
);
int _sprintf_l(
   char *buffer,
   const char *format,
   locale_t locale [,
   argument] ...
);
int swprintf(
   wchar_t *buffer,
   size_t count,
   const wchar_t *format [,
   argument]...
);
int _swprintf_l(
   wchar_t *buffer,
   size_t count,
   const wchar_t *format,
   locale_t locale [,
   argument] ...
);
int __swprintf_l(
   wchar_t *buffer,
   const wchar_t *format,
   locale_t locale [,
   argument] ...
);
template <size_t size>
int sprintf(
   char (&buffer)[size],
   const char *format [,
   argument] ...
); // C++ only
template <size_t size>
int _sprintf_l(
   char (&buffer)[size],
   const char *format,
   locale_t locale [,
   argument] ...
); // C++ only

参数Parameters

bufferbuffer
输出的存储位置Storage location for output

countcount
要存储在 Unicode 版的此函数中的最大字符数。Maximum number of characters to store in the Unicode version of this function.

formatformat
格式控件字符串Format-control string

实际argument
可选自变量Optional arguments

localelocale
要使用的区域设置。The locale to use.

有关更多信息,请参见 格式规范For more information, see Format Specifications.

返回值Return Value

写入的字符数; 如果出现错误,则为-1。The number of characters written, or -1 if an error occurred. 如果缓冲区格式为 null 指针,则将调用无效参数处理程序,如参数验证中所述。If buffer or format is a null pointer, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则这些函数将返回-1,并将errno设置为EINVALIf execution is allowed to continue, these functions return -1 and set errno to EINVAL.

sprintf返回缓冲区中存储的字节数,不包括终止 null 字符。sprintf returns the number of bytes stored in buffer, not counting the terminating null character. swprintf返回存储在缓冲区中的宽字符数,不包括终止 null 宽字符。swprintf returns the number of wide characters stored in buffer, not counting the terminating null wide character.

备注Remarks

Sprintf函数将一系列字符和值存储到缓冲区中。The sprintf function formats and stores a series of characters and values in buffer. 每个自变量(如果有)根据格式规范的相应格式规范进行转换和输出。Each argument (if any) is converted and output according to the corresponding format specification in format. 该格式包括普通字符,其形式和函数与printfformat参数相同。The format consists of ordinary characters and has the same form and function as the format argument for printf. null 字符追加在写入的最后一个字符后。A null character is appended after the last character written. 如果在重叠的字符串之间发生复制,则此行为不确定。If copying occurs between strings that overlap, the behavior is undefined.

重要

使用sprintf,无法限制写入的字符数,这意味着使用sprintf的代码容易受到缓冲区溢出的影响。Using sprintf, there is no way to limit the number of characters written, which means that code using sprintf is susceptible to buffer overruns. 请考虑使用相关的函数_snprintf,该函数指定要写入到缓冲区中的最大字符数,或使用_scprintf来确定需要多大的缓冲区。Consider using the related function _snprintf, which specifies a maximum number of characters to be written to buffer, or use _scprintf to determine how large a buffer is required. 此外,请确保该格式不是用户定义的字符串。Also, ensure that format is not a user-defined string.

swprintfsprintf的宽字符版本;swprintf的指针参数是宽字符字符串。swprintf is a wide-character version of sprintf; the pointer arguments to swprintf are wide-character strings. Swprintf中的编码错误检测可能与sprintf中的错误不同。Detection of encoding errors in swprintf may differ from that in sprintf. swprintffwprintf的行为完全相同,只是swprintf将输出写入字符串而不是类型文件的目标,而swprintf要求使用count参数来指定最大数目要写入的字符数。swprintf and fwprintf behave identically except that swprintf writes output to a string rather than to a destination of type FILE, and swprintf requires the count parameter to specify the maximum number of characters to be written. 这些带有 _l后缀的函数的版本相同,只不过它们使用传入的区域设置参数而不是当前线程区域设置。The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current thread locale.

swprintf符合 ISO C 标准,该标准需要类型为size_t的第二个参数countswprintf conforms to the ISO C Standard, which requires the second parameter, count, of type size_t. 若要强制使用旧的非标准行为,请定义 _CRT_NON_CONFORMING_SWPRINTFSTo force the old nonstandard behavior, define _CRT_NON_CONFORMING_SWPRINTFS. 在未来版本中,可能会删除旧行为,因此应将代码更改为使用新的符合标准行为。In a future version, the old behavior may be removed, so code should be changed to use the new conformant behavior.

在 C++ 中,这些函数具有模板重载,以调用这些函数的更新、更安全副本。In C++, these functions have template overloads that invoke the newer, secure counterparts of these functions. 有关详细信息,请参阅 Secure Template OverloadsFor more information, see Secure Template Overloads.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_stprintf_stprintf sprintfsprintf sprintfsprintf _swprintf_swprintf
_stprintf_l_stprintf_l _sprintf_l_sprintf_l _sprintf_l_sprintf_l __swprintf_l__swprintf_l

要求Requirements

例程所返回的值Routine 必需的标头Required header
sprintf_sprintf_lsprintf, _sprintf_l <stdio.h><stdio.h>
swprintf_swprintf_lswprintf, _swprintf_l <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

示例Example

// crt_sprintf.c
// compile with: /W3
// This program uses sprintf to format various
// data and place them in the string named buffer.

#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 );
}
Output:
   String:    computer
   Character: l
   Integer:   35
   Real:      1.732053

character count = 79

示例Example

// crt_swprintf.c
// wide character example
// also demonstrates swprintf returning error code
#include <stdio.h>

int main( void )
{
   wchar_t buf[100];
   int len = swprintf( buf, 100, L"%s", L"Hello world" );
   printf( "wrote %d characters\n", len );
   len = swprintf( buf, 100, L"%s", L"Hello\xffff world" );
   // swprintf fails because string contains WEOF (\xffff)
   printf( "wrote %d characters\n", len );
}
wrote 11 characters
wrote -1 characters

请参阅See also

流 I/OStream I/O
fprintf、_fprintf_l、fwprintf、_fwprintf_lfprintf, _fprintf_l, fwprintf, _fwprintf_l
printf、_printf_l、wprintf、_wprintf_lprintf, _printf_l, wprintf, _wprintf_l
scanf、_scanf_l、wscanf、_wscanf_lscanf, _scanf_l, wscanf, _wscanf_l
sscanf、_sscanf_l、swscanf、_swscanf_lsscanf, _sscanf_l, swscanf, _swscanf_l
vprintf 函数vprintf Functions