printf %g 是啥?

来源:互联网 发布:nodejs引入js 编辑:程序博客网 时间:2024/04/30 08:04

printf  格式化输出 有很多种可以选择的格式化方式。通常我们最多使用的就是 %s %d %f %c 等。其实还有一些别的选择。配合数字选项,可以灵活实现一些特定的东西。

 

MSDN中的说法:

c int or wint_t When used with printf functions, specifies a single-byte character; when used with wprintf

functions, specifies a wide character. 
C int or wint_t When used with printf functions, specifies a wide character; when used with wprintf functions,

specifies a single-byte character. 
d int Signed decimal integer. 
i int Signed decimal integer. 
o int Unsigned octal integer. 
u int Unsigned decimal integer. 
x int Unsigned hexadecimal integer, using “abcdef.” 
X int Unsigned hexadecimal integer, using “ABCDEF.” 
e double Signed value having the form [ – ]d.dddd e [sign]ddd where d is a single decimal digit, dddd is one or

more decimal digits, ddd is exactly three decimal digits, and sign is + or –. 
E double Identical to the e format except that E rather than e introduces the exponent. 
f double Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of

digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal

point depends on the requested precision. 
g double Signed value printed in f or e format, whichever is more compact for the given value and precision. The e

format is used only when the exponent of the value is less than –4 or greater than or equal to the precision

argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it. 
G double Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate). 
n Pointer to integer Number of characters successfully written so far to the stream or buffer; this value is stored

in the integer whose address is given as the argument. 
p Pointer to void Prints the address pointed to by the argument in the form xxxx:yyyy where xxxx is the segment and

yyyy is the offset, and the digits x and y are uppercase hexadecimal digits. 
s String When used with printf functions, specifies a single-byte–character string; when used with wprintf

functions, specifies a wide-character string. Characters are printed up to the first null character or until the

precision value is reached. 
S String When used with printf functions, specifies a wide-character string; when used with wprintf functions,

specifies a single-byte–character string. Characters are printed up to the first null character or until the

precision value is reached.

 

总结:
%S 用于指定宽字符串。wprintf("%S",(wchar*)str);
%C 也是用于宽字符。  wprintf("%C",(wchar)wc);

%d或%i 有符号的10进制整形 printf("%i",nval);
%u 无符号的10进制整形     printf("%u",unval);

%o 无符号8进制整形            printf("%o",oval);
%x或%X 十六进制小写或大写格式 printf("%2X",hexval);

%f 浮点方式标示               printf("%.2f",fval);   保留小数点后的个数
%e 科学计数法表示             printf("%e",fval);
%E 什么含义?

%g 根据具体的数值选择 %e 或 %f     printf("%.2g",0.0123); ====> 0.012 效果相当于除去0后的有效位数
%G 根据具体数值选择 %E 或 %f

%p 指针形式                   printf("%p",&fval);  等效于 printf("%08X",&fval);

%n 还不知道怎么用

 

其他请补充……

0 0
原创粉丝点击