Usage summary in the C printf function

Function Syntax

stdio.h file definition:

/* Write formatted output to stdout. */
int printf (const char *__restrict __format, ...)

Action: The result is written to standard output formatted

There are two parameters:

The first parameter is a string type constant pointer (read-only variable), shows the format of the output required.

The second parameter "..." indicates formatted variable or constant, may be used as a plurality of intermediate values ​​"," separated, and the value of each parameter should be newly formatted string type and placeholders position correspondence.

Int type return value indicating the number of bytes output

Formatting Syntax
%[flags][width][.precision][length]specifier
%[标志][宽度][.精度][长度]类型
flags
Mark meaning
- Parameters specifying the converted left-justified within its field (default is right-aligned)
+ Specifies the number of the output plus sign in front of
Blank If the first character is not a sign, then in front with a space
0 For numeric conversion, when the length is less than the output field width, padding add leading 0
# Another form of the specified output:
1. If the conversion character is o, then the first digit is 0
2. If the conversion before the characters x or X, a non-zero value in the output is designated by 0x or 0X
3. For transfer character is e, where E, f, g, G, and specify the output always contains a decimal point,
In addition to conversion character is g or G, the tail also specifies the output value will be retained meaningless 0

Note: flags, there may be a plurality of simultaneously and no sequence requirement

The following description by way of example

'-'

// - 指定被转换的参数在其字段内左对齐(默认为右对齐)
short a = 1;
// 将变量a按照10个字符的宽度输出, 默认为右对齐
// 右对齐
printf("%10d\n", a); 
// 加上 "-" 后, 左对齐
printf("%-10d\n", a); 

Export

         1
1         

'+'

// + 指定在输出的数前面加上正负号
short b = 1;
printf("%d\n", b);
// b的值为1, 会将b的值前面隐藏的加号强制显示, 输出 +1
printf("%+d\n", b);
// -b的值为-1, 原样输出 -1
printf("%+d\n", -b);

Export

1
+1
-1

Blank

// 空格 如果第一个字符不是正负号,则在其前面加上一个空格
short d = -1; 
// d的第一个字符是负号, 原样输出-1
printf("% d\n", d); 
// -d的值为1, 不带正负号, 输出 空格+1
printf("% d\n", -d);

Export

-1
 1

0

// 0 对于数值转换,当输出长度小于字段宽度时,添加前导0进行填充
// 1. 数字
short e = 1;
// e的值为1, 要求按照宽度为5输出, 不足的部分宽度内左边补0
// 所以输出 "00001"
printf("%05d\n", e);
// 2. 字符
char f = 'a';
// 将字符型变量f按照宽度为5输出, 不足的部分宽度内左边
// 自动补空格, 所以输出 "    a"
printf("%5c\n", f); 
// 3. 字符串
char *str = "blank";
// str的值为字符串, 需要按照宽度为10输出
// 不足的部分宽度内左边自动补空格, 所以
// 输出 "     blank"
// 注意: 字符串的结束字符 '\0' 不包含在内
printf("%10s\n", str);
// 4. 字符 '\0'
char blk = '\0';
// 打印字符 '\0', 按字符格式会输出 ''(空)
// 按整型格式会输出 "0"
printf("%c, %d\n", blk, blk);
char *blk2 = "hello\0";
// blk2的值包含字符 '\0', 以宽度为10输出
// 不足的部分宽度内自动补空格, 所以会输出
// "     hello", 输出时 '\0' 没有被显示出来
printf("%10s\n", blk2);
// 包含两个 '\0', 输出结果跟上面一样
char *blk3 = "hello\0\0";
printf("%10s\n", blk3);

Export

00001
    a
     blank
, 0
     hello
     hello

'#'

// # 指定另一种输出形式(o, x或X, f, g或G, e或E)
int g = 1000;
// 依次按八进制, 十六进制格式输出
printf("%#od, %#xd, %#Xd\n", g, g, g);
// 将整数1e3, 1000按照科学计数法e, E整型格式输出
printf("%#ed, %#Ed, %#ed\n", 1e3, 1e3, 1000);
// 将浮点数1e-3, 0.001按照科学计数法e, E单精度类型格式输出
printf("%#ef, %#Ef, %#ef\n", 1e-3, 1e-3, 0.001);
// 将浮点数0.001按照单精度f单精度类型格式输出
printf("%#ff\n", 0.001);
// 将浮点数3.14按照双精度g或G双精度类型输出
printf("%#gg, %#GG\n", 3.14, 3.14);

Export

01750d, 0x3e8d, 0X3E8d
1.000000e+03d, 1.000000E+03d, 0.000000e+00d
1.000000e-03f, 1.000000E-03f, 1.000000e-03f
0.001000f
3.14000g, 3.14000G
width

Width is a value field for specifying the minimum width, the width of at least the parameter output the converted value to achieve this, if the number of characters is less than the value of the parameter, the parameters in the left (if the flags set to "-", requires Left if it is on the right) to fill some of the character, the character is usually filled spaces, but if flags is set to 0, the fill character is a digit 0.

/* 宽度(width)  */
char *ur = "human";
// 直接原样输出
printf("%s\n", ur);
// 以宽度为10输出, 同时宽度内右对齐, 左端补空格
printf("%10s\n", ur);
// 以宽度为10输出, 同时宽度内左对齐, 右段补空格
printf("%-10s\n", ur);
// 直接原样输出
printf("%d\n", 2);
// 以宽度为5输出, 同时宽度内右对齐, 左端补空格
printf("%5d\n", 2);
// 以宽度为5输出, 同时宽度内右对齐, 左端补0
printf("%05d\n", 2);
// 以宽度为5输出, 同时宽度内左对齐, 右端补空格
printf("%-5d\n", 2);
// 以宽度为5输出, 同时宽度内左对齐, 右端没有补0, 
// 实际补的是空格, 最后输出 "2    "
printf("%-05d\n", 2);

Export

human
     human
human     
2
    2
00002
2    
2    
.precision

Format "[.precision]" or "[. Precision]" through "." Separated field width and precision

Description:

  • For the string of characters that specifies the maximum number of prints
  • For integer, that specifies the number of digits to be printed (0 necessary, to add padding bits has reached the required width)
  • The number of digits for conversion character is e or E, f, specifies the print of the decimal point
  • For conversion character is g or G, which specifies the print significant digits

The following description by way of example

/* .精度(.precision) */
// 字符串
// 按最大3个字符输出, 超出的部分会被截断丢弃
printf("%.3s\n", "abcdefg");
// 按最大5个字符输出, 小于最大字符个数不会补
// 空格也不会默认右对齐, 会原样输出
printf("%.5s\n", "abc");
// 按最大5个字符输出, 小于最大字符个数不会补
// 空格也不会左对齐, 会原样输出
printf("%-.5s\n", "abc");

// 整数
// 按照整数3位输出, 实际值是5位
// 大于3位, 会原样输出, 不会被截取
printf("%.3d\n", 10000);
// 按照整数5位输出, 实际值是3位
// 不够的位数会从左端开始补0
printf("%.5d\n", 100);

// e或E
// 取小数点之后3位, 多出的部分被截取丢弃, 同时会四舍五入
printf("%.3e, %.3E\n", 1.99991, 1.99991);
// 取小数点之后5位, 不够的部分用0填充
printf("%.5e, %.5E\n", -1.01, -1.01);

// f
// 取小数点之后3位, 多出的部分被截取丢弃, 同时会四舍五入
printf("%.3f\n", 1.2689);
// 取小数点之后5位, 不够的部分用0填充
printf("%.5f\n", 1.26);

// g或G
// 按照3位数字位数输出, 多出的部分被截取丢弃, 同时会四舍五入
// 3代表的是数字位数
printf("%.3g, %.3G\n", 2.888888, 2.888888);
// 按照5位数字位数输出, 不够的部分不会补空格和0
// 5代表的是数字位数
printf("%.5g, %.5G\n", 2.88, 2.88);

Export

abc
abc
abc
10000
00100
2.000e+00, 2.000E+00
-1.01000e+00, -1.01000E+00
1.269
1.26000
2.89, 2.89
2.88, 2.88
length

Length, optional value h, hh, l, ll, L

  • hh represents the corresponding parameters as signed char or unsigned char Type Output
  • h represents the corresponding parameters as output short or unsigned short type
  • l represents the corresponding parameters as long or unsigned long type output
  • ll shows a corresponding parameters as unsigned long long long long or Type Output
  • L represents the corresponding output parameter by a long double

The following description by way of example

/* 长度 length */
// hh signed char, unsigned char
// 输出 ascii 65 对应的 A
printf("%hhc\n", '\x41');
printf("%hhc\n", -1);

// h short, unsigned short
printf("%hd, %hd\n", 1, -1);

// l long, unsigned long
printf("%ld, %ld\n", 1, -1);

// ll long long, unsigned long long
printf("%lld, %lld\n", 1, -1);

// L long double
printf("%Lg, %LG\n", 1, -1);

Export

A
ÿ
1, -1
1, 4294967295
1, 4294967295
-0, 5.12975E-4937
specifier
Conversion character name Explanation
c char character type
d signed decimal integer int
i signed decimal integer int
e double output exponentially single precision, double precision floating point (lowercase e)
E double output exponentially single precision, double precision floating point (uppercase e)
f double precision output unit in decimal form, double precision floating point
g % f% to double or shorter width of the output of the output e single precision, double precision floating point (Index lowercase e)
G % f% to double or shorter width of the output of the output e single precision, double precision floating point (lowercase Index E)
The unsigned int unsigned octal integer (no leading 0)
s char * string
in unsigned decimal integer unsigned int
x unsigned int unsigned hexadecimal integer (without a leading 0x)
X unsigned int unsigned hexadecimal integer (without leading 0X)
p void * pointer value
n int * to store the number of characters have been written
% Parameter conversion is not performed, the display itself%
Escape characters
Escape character Explanation ASCII (decimal)
\a Bell (BEL) 7
\b Backspace (BS), the current position to the previous column 8
\f Feed (FF), the current position to the beginning of the page 12
\n Line feed (LF), the current position to the beginning of the next line 10
\r A carriage return (CR), the current position to the beginning of the line 13
\t Horizontal tabulation (HT), (TAB jumps to the next position) 9
\ v Vertical tabulation (VT) 11
\ Represent a backslash character 92
' On behalf of a single quote character 39
'' It represents a double quote character 34
? On behalf of a question mark 63
\0 空字符(NUL) 0
\ddd 1到3位八进制数所代表的任意字符 三位八进制
\xhh 十六进制所代表的任意字符 十六进制
标准ASCII表及其扩展表

Guess you like

Origin www.cnblogs.com/goujian/p/11783787.html