標籤:
簡述:printf、sprintf函數
轉載自http://www.cnblogs.com/adslg/archive/2008/08/22/1274164.html
printf()函數是格式化輸出函數,一般用於向標準輸出裝置按規定格式輸出資訊,調用格式為:printf("<格式化字串>", <參量表>);其中格式化字串包括兩部分內容:
1.正常字元。這些字元將按原樣輸出;
2.格式化規定字元。以"%"開始,後跟一個或幾個規定字元,用來確定輸出內容格式。
參量表是需要輸出的一系列參數,其個數必須與格式化字串所說明的輸出參數個數一樣多,各參數之間用","隔開,且順序一一對應,否則將會出現意想不到的錯誤。
格式化規定符如下:
%d 十進位有符號整數
%u 十進位不帶正負號的整數
%f 浮點數
%s 字串
%c 字元
%p 指標的值
%e 指數形式的浮點數
%x, %X 十六進位表示的不帶正負號的整數
%0 八進位表示的不帶正負號的整數
%g 自動選擇合適的標記法
1.可以在"%"與字母之間插進數字表示最大場寬,例如"%3d"表示輸出3位整型數,不夠3位靠右對齊;"%9.2f"表示輸出場寬為9的浮點數,其中小數位2位,整數位6位,小數點佔一位,不夠9位靠右對齊;"%8s"表示輸出8個字元的字串,不夠8個字元靠右對齊。
對於整型數或者字串,如果長度超過說明的場寬,則按實際長度輸出;
對於浮點數,如果整數部分的長度超過說明的場寬,則按實際整數位輸出;如果小數部分的長度超過說明的小數位寬度,則按說明的小數位寬度四捨五入輸出。
如果用浮點數表示字元或者整型數的輸出格式,則小數點後的數字代表最大場寬,小數點前的數字代表最小場寬,例如"6.9s"表示顯示一個長度不小於6且不大於9的字串,如果字串長度大於9,則第9個字元之後的內容將被刪除。
2.如果想在輸出值前加一些0,就應在場寬項前加個0,例如"%04d"表示在輸出一個小於4為的整型數時,將在前面補0使其總寬度為4位。
3.可以在"%"與字母之間加小寫字母l表示輸出的是長型數,例如"%ld"表示輸出長整型數,"%lf"表示輸出double類型的浮點數。
4.可以在"%"與字母之間加"-"表示輸出為靠左對齊,例如"%-7d"表示輸出7為整數靠左對齊
一些特殊規定字元
\n 換行
\r 斷行符號
\t Tab符
\f 清屏並換頁
\xhh 用十六進位表示一個ASCII碼,其中hh是1到2個16進位數
#include <stdio.h>#include <string.h>int main(){ char c, s[20], *p; int a = 1234, b=12, *i; float f=3.141592653589f; double x = 0.12345678987654321; p = "How do you do"; strcpy(s, "Hello, Comrade"); i = &b; c = ‘\x41‘; printf("a=%d\n", a); /*結果輸出十進位整數a=1234*/ printf("a=%6d\n", a); /*結果輸出6位十進位數a= 1234*/ printf("a=%06d\n", a); /*結果輸出6位十進位數a=001234*/ printf("a=%2d\n", a); /*a超過2位, 按實際值輸出a=1234*/ printf("*i=%4d\n", *i); /*輸出4位十進位整數*i= 12*/ printf("*i=%-4d\n", *i); /*輸出靠左對齊4位十進位整數*i=12*/ printf("i=%p\n", i); /*輸出地址i=06E4*/ printf("f=%f\n", f); /*輸出浮點數f=3.141593*/ printf("f=6.4f\n", f); /*輸出6位其中小數點後4位的浮點數f=3.1416*/ printf("x=%lf\n", x); /*輸出長浮點數x=0.123457*/ printf("x=%18.16lf\n", x); /*輸出18位其中小數點後16位的長浮點數x=0.1234567898765432*/ printf("c=%c\n", c); /*輸出字元c=A*/ printf("c=%x\n", c); /*輸出字元的ASCII碼值c=41*/ printf("s[]=%s\n", s); /*輸出數組字串s[]=Hello, Comrade*/ printf("s[]=%6.9s\n", s); /*輸出最多9個字元的字串s[]=Hello, Co*/ printf("s=%p\n", s); /*輸出數組字串首字元地址s=FFBE*/ printf("*p=%s\n", p); /* 輸出指標字串p=How do you do*/ printf("p=%p\n", p); /*輸出指標的值p=0194*/ getchar(); return 0; }
/*int sprintf ( char * str, const char * format, ... );Write formatted data to string[將格式化資料寫入到string中]Composes a string with the same text that would be printed if format was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by str.[不同於printf()的將格式化資料輸出到標準輸出裝置上,sprintf()是將格式化資料儲存在一個C字串緩衝中]The size of the buffer should be large enough to contain the entire resulting string (see snprintf for a safer version).[字串緩衝的大小應該大到足以包含所有的結果字串]A terminating null character is automatically appended after the content.[sprintf()函數會自動添加一個null作為字串結束符]After the format parameter, the function expects at least as many additional arguments as needed for format.[sprintf()中參量表的個數需要與格式化規定符的個數一一對應,且數量相等]Return ValueOn success, the total number of characters written is returned. This count does not include the additional null-character automatically appended at the end of the string.[如果成功,則返回被寫入的字元總數(不包含自動添加null結束符)]On failure, a negative number is returned.[如果失敗,則返回一個負數]*/#include <stdio.h>int main(){ char buffer[50]; int n, a=5, b=3; n = sprintf(buffer, "%d plus %d is %d", a, b, a+b); printf("[%s] is a string %d chars long.\n", buffer, n); getchar(); return 0;}
C中的一些函數