linux的格式化输出函数printf与C的行为基本一致。而且很多其他语言的格式化输出也借鉴了C的做法(例如python),因此这里有必要对C的格式化输出做个小结。
一.基本语法:
%[flags][width][.precision][length]specifier
flags有五种:
1. -
表示左对齐,默认是右对齐。
2. +
表示数字前面加正负号,默认情况下正数不加正号。
3.(空格)
如果flag 为空,那么flag自动被填充一个空格, 表示前后的补齐用空格来填充。
4. #
主要针对八进制, 十六进制的输出,在输出前面添加0, 0x, 0X
5. 0
数字左边填充0,而不是默认的空格
width
表示字符的最少宽度,可以是一个数字,如5,表示最少占五个字符;也可以是*,这样的话就必须在后面定义其宽度。
precision
用小数点分开,跟width一样,既可以是数字, 也可以是*号, 同样的,如果是*号,那么后面必须指定宽度。如果指定精度的对象是整数,那么效果相当于制定宽度,并且在前面空白处填充0.
length
定义C的字长,例如Ld指定使用长整数。感觉这个用处不是太大,有需要再来稍微详细写写。
specifier
直接贴一个表
specifier | Output | Example |
---|---|---|
d or i | Signed decimal integer | 392 |
u | Unsigned decimal integer | 7235 |
o | Unsigned octal | 610 |
x | Unsigned hexadecimal integer | 7fa |
X | Unsigned hexadecimal integer (uppercase) | 7FA |
f | Decimal floating point, lowercase | 392.65 |
F | Decimal floating point, uppercase | 392.65 |
e | Scientific notation (mantissa/exponent), lowercase | 3.9265e+2 |
E | Scientific notation (mantissa/exponent), uppercase | 3.9265E+2 |
g | Use the shortest representation: %e or %f | 392.65 |
G | Use the shortest representation: %E or %F | 392.65 |
a | Hexadecimal floating point, lowercase | -0xc.90fep-2 |
A | Hexadecimal floating point, uppercase | -0XC.90FEP-2 |
c | Character | a |
s | String of characters | sample |
p | Pointer address | b8000000 |
n | Nothing 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. | % |
二. 例子
#include<stdio.h>
int main(){
printf ("string:\n");
printf ("%s %10s %-10s %*s\n", "hello", "hello", "hello", 10, "hello");
printf ("character:\n");
printf ("%c %c\n", 'a', 65);
printf ("integer:\n");
printf ("%i %d %.6d %.0d %+i %u\n", 3, 3, 3, 0, 3, 3);
printf ("%x %x %X %#x\n", 5, 10, 10, 10);
printf ("%o %o %#o\n", 8, 8, 8);
printf ("floating point:\n");
printf ("%f %.0f %.20f\n", 2.3, 2.3, 2.3);
printf ("%5.2f %05.2f %-5.2f\n", 2.34, 2.34, 2.34);
printf ("%e %E\n", 31423.234, 31423.234);
printf ("%5.2e %5.0e\n", 3.14159, 3.14159);
printf ("%g %g %G %G\n", 0./0, 1.0/0, 0./0, 1.0/0);
return 0;
}
輸出如下:
string:
hello hello hello hello
character:
a A
integer:
3 3 000003 +3 3
5 a A 0xa
10 10 010
floating point:
2.300000 2 2.29999999999999982236
2.34 02.34 2.34
3.142323e+04 3.142323E+04
3.14e+00 3e+00
-nan inf -NAN INF
三.在其余地方的使用
1.linux 中 printf
例子如下:
printf "%#x\n" 12
printf '%*.*f\n' 10 3 65.3465
printf '%04o\n' 23
printf "%+06d\n" 33
printf "%9.2e\n" 3.1415
输出:
0xc
65.347
0027
+00033
3.14e+00
2.linux 中 awk
awk中内置的printf函数与C的printf函数的行为是一致的。
3.python
附录:
转义字符表
Escape sequence | Hex value in ASCII | Character represented |
---|---|---|
\a | 07 | Alarm (Beep, Bell) |
\b | 08 | Backspace |
\f | 0C | Formfeed |
\n | 0A | Newline (Line Feed); see notes below |
\r | 0D | Carriage Return |
\t | 09 | Horizontal Tab |
\v | 0B | Vertical Tab |
\\ | 5C | Backslash |
\' | 27 | Single quotation mark |
\" | 22 | Double quotation mark |
\? | 3F | Question mark |
\nnn | any | The character whose numerical value is given by nnn interpreted as an octal number |
\xhh… | any | The character whose numerical value is given by hh… interpreted as a hexadecimal number |