資源描述:
《大學(xué)c語言03數(shù)據(jù)輸入與輸出》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、格式:putchar(c)參數(shù):c為字符常量、變量或表達(dá)式功能:把字符c輸出到顯示器上返值:正常,為顯示的代碼值;出錯(cuò),為EOF(-1)第三章數(shù)據(jù)輸入與輸出C語言無I/O語句,I/O操作由函數(shù)實(shí)現(xiàn)#include3.1數(shù)據(jù)輸出字符輸出函數(shù)/*ch3_1.c*/#includemain(){intc;chara;c=65;a='B';putchar(c);putchar('');putchar(a);}運(yùn)行結(jié)果:AB例格式:printf(“格式控制串”,輸出表)功能:按指定格式向顯示器輸出數(shù)據(jù)返值:正常,返回輸出字節(jié)數(shù);出錯(cuò),返回E
2、OF(-1)格式輸出函數(shù)輸出表:要輸出的數(shù)據(jù)(可以沒有,多個(gè)時(shí)以“,”分隔)格式控制串:包含兩種信息格式說明:%[修飾符]格式字符,用于指定輸出格式普通字符或轉(zhuǎn)義序列:原樣輸出格式字符d,ix,Xoucse,Efg%%格式字符:十六進(jìn)制無符號(hào)整數(shù)不帶符號(hào)十進(jìn)制整數(shù)十進(jìn)制整數(shù)指數(shù)形式浮點(diǎn)小數(shù)單一字符字符串八進(jìn)制無符號(hào)整數(shù)小數(shù)形式浮點(diǎn)小數(shù)e和f中較短一種百分號(hào)本身inta=567;printf(“%d”,a);inta=255;printf(“%x”,a);inta=65;printf(“%o”,a);inta=567;printf(“%u”,a);chara=65;pri
3、ntf(“%c”,a);printf(“%s”,“ABC”);floata=567.789;printf(“%e”,a);floata=567.789;printf(“%f”,a);floata=567.789;printf(“%g”,a);printf(“%%”);567ff101567AABC5.677890e+02567.789000567.789%說明格式字符要用小寫格式字符與輸出項(xiàng)個(gè)數(shù)應(yīng)相同,按先后順序一一對(duì)應(yīng)輸出轉(zhuǎn)換:格式字符與輸出項(xiàng)類型不一致,自動(dòng)按指定格式輸出例main(){unsignedintu=65535;printf(”u=%d",u);}輸
4、出結(jié)果:u=-1例inta=3,b=4;printf(“%d%d”,a,b);printf(“a=%d,b=%d”,a,b);例inta=3,b=4;printf(“%d%d”,a,b);printf(“a=%d,b=%d”,a,b);輸出結(jié)果:34a=3,b=4111111111111111165535附加格式說明符(修飾符).n對(duì)實(shí)數(shù),指定小數(shù)點(diǎn)后位數(shù)(四舍五入)對(duì)字符串,指定實(shí)際輸出位數(shù)修飾符功能m輸出數(shù)據(jù)域?qū)?數(shù)據(jù)長度5、空位置自動(dòng)填00在八進(jìn)制和十六進(jìn)制數(shù)前顯示前導(dǎo)0,0x#在d,o,x,u前,指定輸出精度為long型在e,f,g前,指定輸出精度為double型l例inta=1234;floatf=123.456;charch=‘a(chǎn)’;printf(“%8d,%2d”,a,a);printf(“%f,%8f,%8.1f,%.2f,%.2e”,f,f,f,f,f);printf(“%3c”,ch);運(yùn)行1234,1234結(jié)果:123.456000,123.456000,123.5,123.46,1.23e+02a例staticchara[]=“Hello,world!”pri
6、ntf(“%s%15s%10.5s%2.5s%.3s”,a,a,a,a,a);運(yùn)行結(jié)果:Hello,world!Hello,world!HelloHelloHel例m.n例inta=1234;floatf=123.456;staticcharc[]=“Hello,world!”;printf(“%8d,%-8d”,a,a);printf(“%10.2f,%-10.1f”,f,f);printf(“%10.5s,%-10.3s”,c,c);運(yùn)行結(jié)果:1234,1234123.46,123.5Hello,Hel例-例inta=1234;flo
7、atf=123.456;printf(“%08d”,a);printf(“%010.2f”,f);printf(“%0+8d”,a);printf(“0+10.2f”,f);例0、+例inta=123;printf(“%o,%#o,%X,%#X”,a,a,a,a);例#例longa=65536;printf(“%d,%8ld”,a,a);例l00000000000000010000000000000000//00001234//0000123.46//000+1234//000+123.56//173,0173,7