【C语言】printf、scanf打印格式

https://cplusplus.com/reference/cstdio/printf/

C string that contains the text to be written to stdout.
It can optionally contain embedded format specifiers that are replaced by the values specified in subsequent additional arguments and formatted as requested.

format specifier follows this prototype: [see compatibility note below]
%[flags][width][.precision][length]specifier

Where the specifier character at the end is the most significant component, since it defines the type and the interpretation of its corresponding argument:

specifierOutputExample
or iSigned decimal integer392
uUnsigned decimal integer7235
oUnsigned octal610
xUnsigned hexadecimal integer7fa
XUnsigned hexadecimal integer (uppercase)7FA
fDecimal floating point, lowercase392.65
FDecimal floating point, uppercase392.65
eScientific notation (mantissa/exponent), lowercase3.9265e+2
EScientific notation (mantissa/exponent), uppercase3.9265E+2
gUse the shortest representation: %e or %f392.65
GUse the shortest representation: %E or %F392.65
aHexadecimal floating point, lowercase-0xc.90fep-2
AHexadecimal floating point, uppercase-0XC.90FEP-2
cCharactera
sString of characterssample
pPointer addressb8000000
nNothing printed.
The corresponding argument must be a pointer to a signed int.
The number of characters written so far is stored in the pointed location.
%A % followed by another % character will write a single % to the stream.%


The format specifier can also contain sub-specifiers: flagswidth.precision and modifiers (in that order), which are optional and follow these specifications:
 

flagsdescription
-Left-justify within the given field width; Right justification is the default (see width sub-specifier).
+Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.
(space)If no sign is going to be written, a blank space is inserted before the value.
#Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.
Used with a, A, e, E, f, F, g or G it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written.
0Left-pads the number with zeroes (0) instead of spaces when padding is specified (see width sub-specifier).

widthdescription
(number)Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
*The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

.precisiondescription
.numberFor integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.
For a, A, e, E, f and F specifiers: this is the number of digits to be printed after the decimal point (by default, this is 6).
For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
If the period is specified without an explicit value for precision, 0 is assumed.
.*The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.


The length sub-specifier modifies the length of the data type. This is a chart showing the types used to interpret the corresponding arguments with and without length specifier (if a different type is used, the proper type promotion or conversion is performed, if allowed):

specifiers
lengthd iu o x Xf F e E g G a Acspn
(none)intunsigned intdoubleintchar*void*int*
hhsigned charunsigned charsigned char*
hshort intunsigned short intshort int*
llong intunsigned long intwint_twchar_t*long int*
lllong long intunsigned long long intlong long int*
jintmax_tuintmax_tintmax_t*
zsize_tsize_tsize_t*
tptrdiff_tptrdiff_tptrdiff_t*
Llong double

Note regarding the c specifier: it takes an int (or wint_t) as argument, but performs the proper conversion to a char value (or a wchar_t) before formatting it for output.

printf的常用的转义字符及其含义

转义字符

转义字符的意义

ASCII代码

\n

回车换行

10

\t

横向跳到下一制表位置

9

\b

退格

8

\r

回车

13

\f

走纸换页

12

\\

反斜线符"\"

92

\'

单引号符

39

\”

双引号符

34

\a

鸣铃

7

\ddd

1~3位八进制数所代表的字符

\xhh

1~2位十六进制数所代表的字符

 


printf格式字符

格式字符

意义

d

十进制形式输出符号数(正数不输出符号)

o

八进制形式输出符号数(不输出前缀0)

x,X

十六进制形式输出符号数(不输出前缀Ox),用x时输出十六制数a~f时以小写形式输出,用X时,则以大写字母输出

u

输出十进制无符号

f

小数形式输出单、双精度数,隐含输出6位小数,用e时指数以“e”表示(如1.2e+02),用E时指数以“E”表示(如1.2E+02)。

e,E

标准指数形式输出单、双精度数,数字部分小数位数为6位。

g,G

选用%f或%e格式中输出宽度较短的一种格式,不输出无意义的0。用G时,若以指数形式输出,则指数以大写表示。

c

输出单个字符

s

输出字符

printf的标志字符

标 志

意         义

-

结果对齐,右边填空格

+

输出符号(正号或负号)

空格

输出值为正时冠以空格,为负时冠以负号

#

对c,s,d,u类无影响;对o类在输出时加前缀o;对x类在输出时加前缀0x;对e,g,f 类当结果有小数时才给出小数点

 


printf的附加格式说明字符

字符

说明

字母l

用于长整型整型,可加在格式符d、o、x、u前面

m(代表一个正整数)

数据最小宽度

n(代表一个正整数)

对实数,表示输出n位小数;对字符串,表示截取的字符个数

输出的数字或字符在域内向左靠

 

scanf格式字符

字符

说明

d,i

用来输入有符号的十进制整数

u

用来输入无符号的十进制整数

o

用来输入无符号的八进制整数

x,X

用来输入无符号的十六进制整数

c

输入一个字符(只能一个)

s

输入字符串,将字符串送到一个字符数组中,在输入时以非空白字符开始,以第一个空白字符结束,字符串以串结束标志‘\0’作为其最后一个字符

f, e,E ,g,G

输入实数,可以用小数形式或指数形式输入。f, e,E ,g,G可以互换使用。

scanf的附加格式说明字符

字符

说明

字母l

用于长整型整型(可用格式符%ld、%lo、%l x)或double型数据(可用格式符%lf、%l e)

字母h

用于输入短整型(可用格式符%hd、%ho、%hx)

域宽

指定输入数据所占宽度(列数),域宽应为正整数

*

表示本输入项在读入后不赋给相应的变量

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值