本篇内容参考http://blog.csdn.net/jobsss/article/details/8131744
感谢@jobsss
/*
参数说明
sprintf格式规格如下所示。[]中的部分是可选的
%[指定参数][标识符][宽度][.精度]指示符
若想输出'%'本事,请这样'%%'处理
1.处理字符方向。负号表示输出的数字或字符在域内向左靠。
2.填空字元。 0的话表示填0;空格是默认值,表示空格就放着
3.字符总宽度。为最小宽度
4.精确度。指在小数点后面的浮点位数
转换字符
%% 打印百分比符号,不转换
%c 整数转换成对应的ASCII字元
%d 整数转换成十进制
%f 倍精确度数字转换成浮点数
%o 整数转换成八进制
%s 整数转换成字符串
%x 整数转换成小写十六进制
%X 整数转换成大写十六进制
*/
//VC 6.0
//说明:输出1,为了查看空格
#include <stdio.h>
#include <string.h>
#include <iostream.h>
int main(){
char buf[30];
double money = 123.1;
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%06.2f1\n", money);
printf(buf); //123.10
cout<<buf<<endl; //123.10
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%08.2f1\n", money);
printf(buf); //00123.10
cout<<buf<<endl; //00123.10
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%8.2f1\n", money);
printf(buf); // 123.10 前面2个空格
cout<<buf<<endl; // 123.10 前面2个空格
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%-08.2f1\n", money);
printf(buf); //123.10 后面2个空格
cout<<buf<<endl; //123.10 后面2个空格
//%-08.2f 与 %-8.2f 输出结果相同
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%-8.2f1\n", money);
printf(buf); //123.10 后面2个空格
cout<<buf<<endl; //123.10 后面2个空格
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%.2f%%\n", 0.95 * 100);
printf(buf); //95.00 没有输出%
cout<<buf<<endl; //95.00%
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%d\n", 123);
printf(buf); //123
cout<<buf<<endl; //123
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%8d%8d\n", 123, 4567);
printf(buf); // 123 4567说明:123前面5个空格,4567前面4个空格
cout<<buf<<endl; // 123 4567
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%-8d%-8d%d\n", 123, 4567, 1);
printf(buf); //123 4567 1说明:123后面5个空格,4567后面4个空格
cout<<buf<<endl; //123 4567 1
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%8x\n", 4567);
printf(buf); // 11d7 前面4个空格
cout<<buf<<endl; // 11d7
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%-8X%d\n", 4567, 1);
printf(buf); //11D7 1 后面4个空格
cout<<buf<<endl; //11D7 1
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%08X\n", 4567);
printf(buf); //000011D7
cout<<buf<<endl; //000011D7
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%08d\n", 4567);
printf(buf); //00004567
cout<<buf<<endl; //00004567
short si = -1;
memset(buf, 0x00, sizeof(buf));
sprintf(buf,"%04X\n", si);
printf(buf); //FFFFFFFF
cout<<buf<<endl; //FFFFFFFF
memset(buf, 0x00, sizeof(buf));
sprintf(buf,"%04X\n", (unsigned short)si);
printf(buf); //FFFF
cout<<buf<<endl; //FFFF
unsigned short sii = -1;
memset(buf, 0x00, sizeof(buf));
sprintf(buf,"%04X\n", sii);
printf(buf); //FFFF
cout<<buf<<endl; //FFFF
//浮点数使用格式符"%f"控制,默认保留小数点后6位数字
memset(buf, 0x00, sizeof(buf));
sprintf(buf,"%f\n", 3.1415926);
printf(buf); //3.141593
cout<<buf<<endl; //3.141593
//"%m.nf" m:打印宽度 n:小数位数
memset(buf, 0x00, sizeof(buf));
sprintf(buf,"%10.3f1\n", 3.1415926);
printf(buf); // 3.142 前面5个空格
cout<<buf<<endl; // 3.142
memset(buf, 0x00, sizeof(buf));
sprintf(buf,"%-10.3f1\n", 3.1415926);
printf(buf); //3.142 1后面5个空格
cout<<buf<<endl; //3.142 1
memset(buf, 0x00, sizeof(buf));
sprintf(buf,"%.3f1\n", 3.1415926);
printf(buf); //3.142
cout<<buf<<endl; //3.142
int ii = 100;
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%.2f\n", ii);
printf(buf); //0.00
cout<<buf<<endl; //0.00
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%.2f\n", double(ii));
printf(buf); //100.00
cout<<buf<<endl; //100.00
//"#"与"%X"合用时自动为16进制数增加"0X"前缀
for(int i = 32; i< 127; i++){
printf("[ %c ]:%3d %#04X\n",i,i,i);
}
cout<<endl;
/*
[ ]: 32 0X20
[ ! ]: 33 0X21
[ " ]: 34 0X22
[ # ]: 35 0X23
[ $ ]: 36 0X24
[ % ]: 37 0X25
[ & ]: 38 0X26
[ ' ]: 39 0X27
[ ( ]: 40 0X28
[ ) ]: 41 0X29
[ * ]: 42 0X2A
[ + ]: 43 0X2B
[ , ]: 44 0X2C
[ - ]: 45 0X2D
[ . ]: 46 0X2E
[ / ]: 47 0X2F
[ 0 ]: 48 0X30
[ 1 ]: 49 0X31
[ 2 ]: 50 0X32
[ 3 ]: 51 0X33
[ 4 ]: 52 0X34
[ 5 ]: 53 0X35
[ 6 ]: 54 0X36
[ 7 ]: 55 0X37
[ 8 ]: 56 0X38
[ 9 ]: 57 0X39
[ : ]: 58 0X3A
[ ; ]: 59 0X3B
[ < ]: 60 0X3C
[ = ]: 61 0X3D
[ > ]: 62 0X3E
[ ? ]: 63 0X3F
[ @ ]: 64 0X40
[ A ]: 65 0X41
[ B ]: 66 0X42
[ C ]: 67 0X43
[ D ]: 68 0X44
[ E ]: 69 0X45
[ F ]: 70 0X46
[ G ]: 71 0X47
[ H ]: 72 0X48
[ I ]: 73 0X49
[ J ]: 74 0X4A
[ K ]: 75 0X4B
[ L ]: 76 0X4C
[ M ]: 77 0X4D
[ N ]: 78 0X4E
[ O ]: 79 0X4F
[ P ]: 80 0X50
[ Q ]: 81 0X51
[ R ]: 82 0X52
[ S ]: 83 0X53
[ T ]: 84 0X54
[ U ]: 85 0X55
[ V ]: 86 0X56
[ W ]: 87 0X57
[ X ]: 88 0X58
[ Y ]: 89 0X59
[ Z ]: 90 0X5A
[ [ ]: 91 0X5B
[ \ ]: 92 0X5C
[ ] ]: 93 0X5D
[ ^ ]: 94 0X5E
[ _ ]: 95 0X5F
[ ` ]: 96 0X60
[ a ]: 97 0X61
[ b ]: 98 0X62
[ c ]: 99 0X63
[ d ]:100 0X64
[ e ]:101 0X65
[ f ]:102 0X66
[ g ]:103 0X67
[ h ]:104 0X68
[ i ]:105 0X69
[ j ]:106 0X6A
[ k ]:107 0X6B
[ l ]:108 0X6C
[ m ]:109 0X6D
[ n ]:110 0X6E
[ o ]:111 0X6F
[ p ]:112 0X70
[ q ]:113 0X71
[ r ]:114 0X72
[ s ]:115 0X73
[ t ]:116 0X74
[ u ]:117 0X75
[ v ]:118 0X76
[ w ]:119 0X77
[ x ]:120 0X78
[ y ]:121 0X79
[ z ]:122 0X7A
[ { ]:123 0X7B
[ | ]:124 0X7C
[ } ]:125 0X7D
[ ~ ]:126 0X7E
*/
for(i = 32; i< 127; i++){
printf("[ %c ]:%3d %#04x\n",i,i,i);
} //0x x小写
cout<<endl;
char a1[] = {'A','B','C','D','E','F','G'};
char a2[] = {'H','I','J','K','L','M','N'};
//"%m.ns" m:占用宽度(长度不足补空格,超出按实际宽度打印)
//n:相应的字符串最多取的字符数
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%.7s%.7s\n", a1, a2);
printf(buf); //ABCDEFGHIJKLMN
cout<<buf<<endl; //ABCDEFGHIJKLMN
//sprintf 采用"*"来占用一个本来需要一个指定宽度或精度的常数数字的位置
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%.*s%.*s\n", sizeof(a1), a1, sizeof(a2), a2);
printf(buf); //ABCDEFGHIJKLMN
cout<<buf<<endl; //ABCDEFGHIJKLMN
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%-*d?\n", 4, 'A');
printf(buf); //65 ? 左补2位空格
cout<<buf<<endl; //65 ?
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%#0*X\n", 8, 128);
printf(buf); //0X000080
cout<<buf<<endl; //0X000080
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%*.*f\n", 10, 2, 3.1415926);
printf(buf); // 3.14 前面6个空格
cout<<buf<<endl; // 3.14
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%u\n", &i);
printf(buf); //1244944
cout<<buf<<endl; //1244944
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%08X\n", &i);
printf(buf); //0012FF10
cout<<buf<<endl; //0012FF10
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%8X\n", &i);
printf(buf); // 12FF10 前面2个空格
cout<<buf<<endl; // 12FF10
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%p\n", &i);
printf(buf); //0012FF10
cout<<buf<<endl; //0012FF10
memset(buf, 0x00, sizeof(buf));
sprintf(buf, "%0*x\n", 2*sizeof(void *), &i); //指针占4个字节
printf(buf); //0012ff10
cout<<buf<<endl; //0012ff10
//spritnf 返回了本次函数调用最终打印到字符缓冲区中的字符数目。
return 0;
}