printf之變數參數

轉 換 字 元

以何種形式列印對應的引數

c

字元

d or i

10進位整數

o

無正負號8進位整數

x

無正負號16進位整數

f

浮點數,例如 :7.123000

s

字串

 

 

變數宣告及設定初值  

char          c = 'w';  
int             i = 1, j = 29;
float         x = 333.12345678901234567890; 
double        y = 333.12345678901234567890;
static char   s1[] = "she sells sea shells" ; 
static char   s2[] = "by the seashore";

 

格式

運算式

列印在欄位上的結果

注 解

%c

c

"w"

預設的欄度長度為1

%2c

c

" w"

向右對齊

%-3c

c

"w  "

向左對齊

%d

 -j

"-29"

預設的欄度長度為3

%010d

i

"0000000001"

以0填補

%-12d

j

"29          "

向左對齊

%12o

j

"          35"

8進位 ,向右對齊

%-12x

j

"1d          "

16進位 ,向左對齊

%f

x

"333.123456"

預設的精確度為6

%.1f

x

"333.1"

精確度為1

%20.3f

x

"             333.123"

向右對齊

%.9f

y

"333.123456789"

精確度為9

%s

s1

"she sells sea shells"

預設的欄度長度為20

%7s

s1

"she sells sea shells"

需要更多的空間

%.5s

s2

"by th"

精確度為5

%-15.12s

s2

"by the seash   "

精確度為12 ,向左對齊

 
 
arrow
arrow
    文章標籤
    printf之變數參數
    全站熱搜

    JL8051 發表在 痞客邦 留言(0) 人氣()