sprintf 使用说明

来源:互联网 发布:mysql 加列 编辑:程序博客网 时间:2024/05/19 08:38

#include <stdio.h>

函数原型:

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

注意这里的buffer指针 指向的是格式化字符后写入的首地址

说明文档上的描述:  Write formatted data to a string. These functions are deprecated because more secure versions are available

see sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l.

意思就是:格式化数据,并写入字符串,这些方法已经不用,因为有更安全的方法可用

参考sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l.这些函数

sprintf_s函数原型

int sprintf_s( char *buffer, size_t sizeOfBuffer, const char *format [, argument] ... );

从原型上看:sprintf  和 sprintf_s 的差别就是sprintf_s多了一个参数sizeofbuffer

两个的具体差别,还是看MSDN上的说明:

One main difference between sprintf_s and sprintf is thatsprintf_s checks the format string for valid formatting characters, whereassprintf only checks if the format string or buffer areNULL pointers. If either check fails, the invalid parameter handler is invoked, as described inParameter Validation. If execution is allowed to continue, the function returns -1 and setserrno toEINVAL.

The other main difference between sprintf_s and sprintf is thatsprintf_s takes a length parameter specifying the size of the output buffer in characters. If the buffer is too small for the text being printed then the buffer is set to an empty string and the invalid parameter handler is invoked. Unlikesnprintf,sprintf_sguarantees that the buffer will be null-terminated (unless the buffer size is zero).

一个差别是:sprintf_s 会检查格式化字符的合法性,而sprintf只会检查其是否是空指针

另一个差别是:sprintf_s 设置了输出buffer的大小

sprintf使用实例: 

// crt_sprintf.c// compile with: /W1// 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

Output:   String:    computer   Character: l   Integer:   35   Real:      1.732053character count = 79

sprintf函数是一个变参函数,前两个函数有固定类型,会进行安全检查,后面的参数都不是类型安全的。使用的时候要小心了 

摘录网上的一个例子来说明:

假如我们想打印短整数(short)-1的内存16 进制表示形式,在Win32 平台上,一个short 型占2 个字节,所以我们自然希望用4 个16 进制数字来打印它:
short si = -1;

sprintf(s, "%04X", si);

产生“FFFFFFFF”,怎么回事?因为spritnf 是个变参函数,除了前面两个参数之外,后面的参数都不是类型安全的,函数更没有办法仅仅通过一个“%X”就能得知当初函数调用前参数压栈时被压进来的到底是个4 字节的整数还是个2 字节的短整数,所以采取了统一4 字节的处理方式,导致参数压栈时做了符号扩展,扩展成了32 位的整数-1,打印时4 个位置不够了,就把32 位整数-1 的8 位16 进制都打印出来了。如果你想看si 的本来面目,那么就应该让编译器做0 扩展而不是符号扩展(扩展时二进制左边补0 而不是补符号位):

sprintf(s, "%04X", (unsigned short)si);就可以了。

或者:unsigned short si = -1;sprintf(s, "%04X", si);


用法 :

1.将10进制数字,转换为16进制字符串

sprintf(s, "%08X", 5678);          //产生:"0000162E"

2.浮点数的打印和格式控制

sprintf(s, "%m.nf", 3.1415926);    // m:代表打印的总长度,n代表小数位     默认n为6

3.连接字符串

sprintf(s, "%m.ns%m.ns",cArray1,cArray2)   //m:代表打印的总长度  n:代表取的字节长度

0 0
原创粉丝点击