printf 格式化输出

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
printf函数用于格式化输出,可以根据指定的格式将变量的值打印到屏幕上。 引用中的例子展示了如何使用printf函数来格式化输出一个整数。通过使用"%d"、"%o"和"%x"等格式指示符,可以将整数以十进制、八进制或十六进制形式打印出来。 引用中的例子展示了如何使用"%o"格式指示符来以八进制形式输出一个整数。在例子中,将-1转换为八进制数时,得到了177777。 引用中的例子展示了如何使用printf函数来格式化输出浮点数。通过使用"%f"格式指示符,可以将浮点数打印到屏幕上。 总结起来,printf函数可以通过不同的格式指示符来格式化输出不同类型的变量,包括整数和浮点数。这样可以使输出更加灵活和易读。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [C语言输入输出(左端对齐,右端对齐,对齐补零……)](https://blog.csdn.net/m0_64782700/article/details/123153448)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [printf格式化输出](https://blog.csdn.net/sinat_22991367/article/details/78997945)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值