[MSDN]sprintf, swprintf

sprintf, swprintf
sprintf, swprintf 函数

Write formatted data to a string.
往缓冲区中按指定格式输出参数

int sprintf( char *buffer, const char *format [, argument] ... );

int swprintf( wchar_t *buffer, const wchar_t *format [, argument] ... );

RoutineRequired HeaderCompatibility
sprintf<stdio.h>ANSI, Win 95, Win NT
swprintf<stdio.h> or <wchar.h>ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIBSingle thread static library, retail version
LIBCMT.LIBMultithread static library, retail version
MSVCRT.LIBImport library for MSVCRT.DLL, retail version

Return Value
返回值

sprintf returns the number of bytes stored in buffer, not counting the terminating null character.
sprintf 返回 buffer 字符串的存储的字节数,不包括终结符。

swprintf
returns the number of wide characters stored in buffer, not counting the terminating null wide character.
swprintf  返回 buffer 的宽度,不包括

Parameters

buffer

Storage location for output

format

Format-control string

argument

Optional arguments

For more information, see Format Specifications.

Remarks

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. The format consists of ordinary characters and has the same form and function as the format argument for printf. A null character is appended after the last character written. If copying occurs between strings that overlap, the behavior is undefined.

swprintf is a wide-character version of sprintf; the pointer arguments to swprintf are wide-character strings. Detection of encoding errors in swprintf may differ from that in sprintf. swprintf and fwprintf behave identically except that swprintf writes output to a string rather than to a destination of type FILE.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined_MBCS Defined_UNICODE Defined
_stprintfsprintfsprintfswprintf

Example

/* SPRINTF.C: This program uses sprintf to format various
 * data and place them in the string named buffer.
 */

#include <stdio.h>

void 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,     "\tString:    %s\n", s );   
   // 两个转义字符:2,"String:”:7,"computer":8,再加4个空格:所以一共是:21个字节   j += sprintf( buffer + j, "\tCharacter: %c\n", c );   j += sprintf( buffer + j, "\tInteger:   %d\n", i );   j += sprintf( buffer + j, "\tReal:      %f\n", fp );   printf( "Output:\n%s\ncharacter count = %d\n", buffer, j ); }

Output

Output:
   String:    computer
   Character: l
   Integer:   35
   Real:      1.732053

character count = 71

Stream I/O Routines

See Also   _snprintf, fprintf, printf, scanf, sscanf, vprintf Functions


Send feedback to MSDN. Look here for MSDN Online resources.

转载于:https://www.cnblogs.com/ctoroad/archive/2006/05/09/395574.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值