printf( ) 的列印格式、控制字元、修飾子

10 六月, 2007 at 10:08 下午 9 留言

列印格式 輸出敘述
%c 字元
%s 字串
%d 十進位整數
%u 無號十進位整數
%o 無號八進位整數
%x 無號十六進位整數,以 0 ~ f 表示
%X 無號十六進位整數,以 0 ~ F 表示
%f 浮點數,小數點型式
%e 浮點數,指數e型式
%E 浮點數,指數E型式
%g 印出 %f %e 較短者
%G 印出 %F %E 較短者
%p 指標位址
%% 印出百分比符號

控制字元 功能
\a 警告音
\b 倒退
\f 換頁
\n 換行
\r 歸位
\t 跳格
\’ 印出單引號
\" 印出雙引號
\\ 反斜線
\/ 斜線
\d 八進位 Ascii 碼
\x 十六進位 Ascii 碼

修飾子 功能 範例
向左對齊 %-3d
+ 將數值的正負號顯示出來 %+5d
空白 數值為正值時,留一格空白;為負值時,顯示負號 % 6f
0 將固定欄位長度的數值前空白處填上 0;與 – 修飾子同時使用時,此修飾子無效 %07.2f
數字 欄位長度,當數值的位數大於所定的欄位長度時,欄位會自動加寬它的長度 %9d
. 數值以 %e, %E, %f 型式表示時,決定小數點後所要顯示的位數 %4.3f
h 表示 short int 或是 unsigned short int %5h
l 表示 long int 或是 unsigned long int %lu

Entry filed under: C, IT.

C 語言資料型態 運算子的優先順序

9 則迴響 Add your own

  • 1. 初學者  |  28 五月, 2009 於 7:51 下午

    這資料正好是我需要的XD
    謝謝你喔~~

    回應
  • 2. C drokner  |  23 七月, 2010 於 4:20 下午

    Useful and clear howto-like explanation.
    Many Thanks

    回應
  • 3. The C Programming Language Chapter 1 Ex 1-10 « 盲頭烏蠅  |  16 七月, 2011 於 12:21 上午

    […] 2. Experiment to find out what happens when printf’s argument string contins c, where c is some character not listed above. Complete list for the argument string in printf […]

    回應
  • 4. Guest  |  25 十月, 2011 於 4:49 下午

    常常忘記, 卻又很需要的資訊, 感謝!!

    回應
  • 5. 路人乙  |  7 九月, 2013 於 2:28 下午

    可以引用在我的網站嗎?

    回應
  • 6. old.bbclubhk.com  |  18 九月, 2013 於 4:47 下午

    Usually I don’t learn post on blogs, however I
    would like to say that this write-up very forced me to take a look at
    and do so! Your writing style has been surprised me.
    Thank you, quite great post.

    回應
  • 7. french beard pics  |  10 四月, 2014 於 8:14 上午

    This is a very good tip especially to those fresh to the
    blogosphere. Brief but very precise information… Many thanks
    for sharing this one. A must read post!

    回應
  • 8. Erick  |  11 七月, 2014 於 2:37 下午

    #include
    int main()
    {
    int i=43;
    printf(“%d\n", i);
    printf(“%d\n",printf(“%d", i));
    printf(“%d\n",printf(“%d",printf(“%d", i))) ;
    return 0;
    }
    能顺带解释一下上面的代码输出吗?
    谢谢!!!

    回應

發表留言

Trackback this post  |  Subscribe to the comments via RSS Feed


六月 2007
 123
45678910
11121314151617
18192021222324
252627282930