c中printf()方法中%*的格式及含义

本文详细介绍了C语言中printf函数的格式化输出方法,包括各种类型的数据如何使用不同的格式化指令进行输出,以及如何指定输出宽度和精度等高级用法。
CodeFormat
%ccharacter
%dsigned integers
%isigned integers
%escientific notation, with a lowercase "e"
%Escientific notation, with a uppercase "E"
%ffloating point
%guse %e or %f, whichever is shorter
%Guse %E or %f, whichever is shorter
%ooctal
%sa string of characters
%uunsigned integer
%xunsigned hexadecimal, with lowercase letters
%Xunsigned hexadecimal, with uppercase letters
%pa pointer
%nthe argument shall be a pointer to an integer into which is placed the number of characters written so far
%%a '%' sign

An integer placed between a % sign and the format command acts as a minimum field width specifier, and pads the output with spaces or zeros to make it long enough. If you want to pad with zeros, place a zero before the minimum field width specifier:

   %012d                

You can also include a precision modifier, in the form of a .N where N is some number, before the format command:

   %012.4d              

The precision modifier has different meanings depending on the format command being used:

  • With %e, %E, and %f, the precision modifier lets you specify the number of decimal places desired. For example, %12.6f will display a floating number at least 12 digits wide, with six decimal places.
  • With %g and %G, the precision modifier determines the maximum number of significant digits displayed.
  • With %s, the precision modifer simply acts as a maximumfield length, to complement the minimum field length that precedes the period.

All of printf()'s output is right-justified, unless you place a minus sign right after the % sign. For example,

   %-12.4f              

will display a floating point number with a minimum of 12 characters, 4 decimal places, and left justified. You may modify the %d, %i, %o, %u, and %x type specifiers with the letter l and the letter h to specify long and short data types (e.g. %hd means a short integer). The %e, %f, and %g type specifiers can have the letter l before them to indicate that a double follows. The %g, %f, and %e type specifiers can be preceded with the character '#' to ensure that the decimal point will be present, even if there are no decimal digits. The use of the '#' character with the %x type specifier indicates that the hexidecimal number should be printed with the '0x' prefix. The use of the '#' character with the %o type specifier indicates that the octal value should be displayed with a 0 prefix.

转载于:https://www.cnblogs.com/vilyLei/articles/1682167.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值