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不会计数参数。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 并设置errnoEINVALIf 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. 该格式包括普通字符,其形式和函数与相同格式参数printfThe 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.

swprintf是宽字符版本sprintf; 的指针参数swprintf都是宽字符字符串。swprintf is a wide-character version of sprintf; the pointer arguments to swprintf are wide-character strings. 检测到的编码中的错误swprintf可能有所不同,在sprintfDetection of encoding errors in swprintf may differ from that in sprintf. swprintffwprintf行为方式相同,只不过swprintf将输出写入到一个字符串,而不是类型的目标文件,和swprintf要求计数参数来指定要写入的字符的最大数。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_tswprintf 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