格式化输出小结

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

直接贴一个表

specifierOutputExample
d 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.
 
%% 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
\a07Alarm (Beep, Bell)
\b08Backspace
\f0CFormfeed
\n0ANewline (Line Feed); see notes below
\r0DCarriage Return
\t09Horizontal Tab
\v0BVertical Tab
\\5CBackslash
\'27Single quotation mark
\"22Double quotation mark
\?3FQuestion mark
\nnnanyThe character whose numerical value is given by nnn interpreted as an octal number
\xhh…anyThe character whose numerical value is given by hh… interpreted as a hexadecimal number

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值