格式化输出小结

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
    评论
在Python中,可以使用time和calendar模块来格式化日期和时间。时间间隔是以秒为单位的浮点小数,每个时间戳都以自从格林威治时间1970年01月01日00时00分00秒起经过了多长时间来表示。\[1\] 另外,Python中的datetime、date和time类都提供了strftime()方法,该方法接收一个格式字符串,用于输出日期时间的字符串表示。下面是一个使用例子:\[2\] ```python from datetime import * tm = time(23, 46, 10) print 'tm:', tm print 'hour: %d, minute: %d, second: %d, microsecond: %d' % (tm.hour, tm.minute, tm.second, tm.microsecond) tm1 = tm.replace(hour = 20) print 'tm1:', tm1 print 'isoformat():', tm.isoformat() ``` 运行上述代码,将会得到以下结果: ``` tm: 23:46:10 hour: 23, minute: 46, second: 10, microsecond: 0 tm1: 20:46:10 isoformat(): 23:46:10 ``` 这个例子展示了如何创建一个时间对象,并使用strftime()方法来格式化时间。你可以根据自己的需求,使用不同的格式字符串来输出不同的时间格式。 #### 引用[.reference_title] - *1* [你想要知道的Python日期格式化知识都在这](https://blog.csdn.net/LLC25802580/article/details/123095230)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [python datetime处理时间小结](https://blog.csdn.net/marraybug/article/details/117964788)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值