C語言printf()函數(shù):格式化輸出函數(shù)
printf()函數(shù)是最常用的格式化輸出函數(shù),其原型為:
int printf( char * format, ... );
printf()會根據(jù)參數(shù) format 字符串來轉換并格式化數(shù)據(jù),然后將結果輸出到標準輸出設備(顯示器),直到出現(xiàn)字符串結束('/0')為止。
參數(shù) format 字符串可包含下列三種字符類型:
格式轉換為一個百分比符號(%)及其后的格式字符所組成。一般而言,每個%符號在其后都必需有一個參數(shù)與之相呼應(只有當%%轉換字符出現(xiàn)時會直接輸出%字符),而欲輸出的數(shù)據(jù)類型必須與其相對應的轉換字符類型相同。
printf()格式轉換的一般形式如下:
%(flags)(width)(. prec)type
以括號括起來的參數(shù)為選擇性參數(shù),而%與type 則是必要的,下面介紹 type 的幾種形式。
1) 整數(shù)
2) 字符及字符串
prec 有幾種情況:
width 為參數(shù)的最小長度,若此欄并非數(shù)值,而是*符號,則表示以下一個參數(shù)當做參數(shù)長度。
flags 有下列幾種情況
【返回值】成功則返回寫入的字符數(shù)目。
如果發(fā)生寫入錯誤,將會設置文件錯誤標志(可通過 ferror() 檢測),并返回一個負數(shù)。
如果在寫入寬字符時一個多字節(jié)的字符發(fā)生編碼錯誤,那么 errno 將被設置為 EILSEQ,并返回一個負數(shù)。
printf( format, ... ) 等價于 fprintf(stdout, format, ...),更多信息請參考 fprintf() 函數(shù)。
【實例】分別輸出整數(shù)、浮點數(shù)和字符串。
#include<stdio.h>int main(void){ int a=1; float b=5.0; char str[100]= ""; scanf("%c %c %c",&a,&b,str); /*分別演示 整數(shù)*/ printf("int is:%d/n",a); /*分別演示 浮點數(shù)*/ printf("float is:%f/n",b); /*分別演示 字符串*/ printf("char is:%s/n",str); return 0;}
輸出結果:
【運行結果】
1 4.4 fsint is:1float is:4.400000char is:fs
例子首先是等待用戶輸入整數(shù)浮點數(shù)和一個字符串,然后調(diào)用函數(shù)printf()按照對應的格式輸出。
又如,輸出更多格式的數(shù)據(jù)。
#include <stdio.h>int main(){ printf ("Characters: %c %c /n", 'a', 65); printf ("Decimals: %d %ld/n", 1977, 650000L); printf ("Preceding with blanks: %10d /n", 1977); printf ("Preceding with zeros: %010d /n", 1977); printf ("Some different radices: %d %x %o %#x %#o /n", 100, 100, 100, 100, 100); printf ("floats: %4.2f %+.0e %E /n", 3.1416, 3.1416, 3.1416); printf ("Width trick: %*d /n", 5, 10); printf ("%s /n", "A string"); return 0;}
輸出結果:
Characters: a ADecimals: 1977 650000Preceding with blanks: 1977Preceding with zeros: 0000001977Some different radices: 100 64 144 0x64 0144floats: 3.14 +3e+000 3.141600E+000Width trick: 10A string
C語言fprintf()函數(shù):輸出函數(shù)(格式化輸出數(shù)據(jù)至文件)
頭文件:
#include <stdio.h>
定義函數(shù):
int fprintf(FILE * stream, const char * format, ...);
函數(shù)說明:fprintf()會根據(jù)參數(shù)format 字符串來轉換并格式化數(shù)據(jù), 然后將結果輸出到參數(shù)stream 指定的文件中, 直到出現(xiàn)字符串結束('/0')為止。
返回值:關于參數(shù)format 字符串的格式請參考printf(). 成功則返回實際輸出的字符數(shù), 失敗則返回-1, 錯誤原因存于errno 中.
范例
#include <stdio.h>main(){ int i = 150; int j = -100; double k = 3.14159; fprintf(stdout, "%d %f %x /n", j, k, i); fprintf(stdout, "%2d %*d/n", i, 2, i);}
執(zhí)行:
-100 3.141590 96150 150
C語言sprintf()函數(shù):將格式化的數(shù)據(jù)寫入字符串
頭文件:
#include <stdio.h>
sprintf()函數(shù)用于將格式化的數(shù)據(jù)寫入字符串,其原型為:
int sprintf(char *str, char * format [, argument, ...]);
【參數(shù)】str為要寫入的字符串;format為格式化字符串,與printf()函數(shù)相同;argument為變量。
除了前兩個參數(shù)類型固定外,后面可以接任意多個參數(shù)。而它的精華,顯然就在第二個參數(shù)--格式化字符串--上。 printf()和sprintf()都使用格式化字符串來指定串的格式,在格式串內(nèi)部使用一些以“%”開頭的格式說明符(format specifications)來占據(jù)一個位置,在后邊的變參列表中提供相應的變量,最終函數(shù)就會用相應位置的變量來替代那個說明符,產(chǎn)生一個調(diào)用者想要的字符串。
sprintf()最常見的應用之一莫過于把整數(shù)打印到字符串中,如:
sprintf的作用是將一個格式化的字符串輸出到一個目的字符串中,而printf是將一個格式化的字符串輸出到屏幕。sprintf的第一個參數(shù)應該是目的字符串,如果不指定這個參數(shù),執(zhí)行過程中出現(xiàn) "該程序產(chǎn)生非法操作,即將被關閉...."的提示。
sprintf()會根據(jù)參數(shù)format 字符串來轉換并格式化數(shù)據(jù),然后將結果復制到參數(shù)str 所指的字符串數(shù)組,直到出現(xiàn)字符串結束('/0')為止。關于參數(shù)format 字符串的格式請參考printf()。
【返回值】成功則返回參數(shù)str 字符串長度,失敗則返回-1,錯誤原因存于errno 中。
注意:C語言對數(shù)組進行操作時并不檢測數(shù)組的長度,如果str的長度不夠,sprintf()很容易造成緩沖區(qū)溢出,帶來意想不到的后果,黑客經(jīng)常利用這個弱點攻擊看上去安全的系統(tǒng)。請看下面的代碼:
#include <stdio.h>main(){ char buf[10]; sprintf(buf, "The length of the string is more than 10"); printf("%s", buf);}
編譯并運行,屏幕上輸出”The length of the string is more than 10“,同時系統(tǒng)提示程序已經(jīng)停止。原因就是要寫入的字符串的長度超過了buf的長度,造成緩沖區(qū)溢出。
使用snprintf()來代替sprintf()將能夠很好的解決這個問題。
【實例】打印字母a的ASCII值。
#include <stdio.h>main(){ char a = 'a'; char buf[80]; sprintf(buf, "The ASCII code of a is %d.", a); printf("%s", buf);}
運行結果:
The ASCII code of a is 97.
又如,產(chǎn)生10個100以內(nèi)的隨機數(shù)并輸出。
#include<stdio.h>#include<stdlib.h>#include<time.h>int main(void){ char str[100]; int offset =0; int i=0; srand(time(0)); // *隨機種子 for(i = 0;i<10;i++) { offset+=sprintf(str+offset,"%d,",rand()%100); // 格式化的數(shù)據(jù)寫入字符串 } str[offset-1]='/n'; printf(str); return 0;}
運行結果:
74,43,95,95,44,90,70,23,66,84
例子使用了一個新函數(shù)srand(),它能產(chǎn)生隨機數(shù)。例子中最復雜的部分是for循環(huán)中每次調(diào)用函數(shù)sprintf()往字符數(shù)組寫數(shù)據(jù)的時候,str+foffset為每次寫入數(shù)據(jù)的開始地址,最終的結果是所有產(chǎn)生的隨機數(shù)據(jù)都被以整數(shù)的形式存入數(shù)組中。
新聞熱點
疑難解答
圖片精選