C程序设计ch输入与输出操作.ppt

上传人:小飞机 文档编号:5426152 上传时间:2023-07-05 格式:PPT 页数:19 大小:266KB
返回 下载 相关 举报
C程序设计ch输入与输出操作.ppt_第1页
第1页 / 共19页
C程序设计ch输入与输出操作.ppt_第2页
第2页 / 共19页
C程序设计ch输入与输出操作.ppt_第3页
第3页 / 共19页
C程序设计ch输入与输出操作.ppt_第4页
第4页 / 共19页
C程序设计ch输入与输出操作.ppt_第5页
第5页 / 共19页
点击查看更多>>
资源描述

《C程序设计ch输入与输出操作.ppt》由会员分享,可在线阅读,更多相关《C程序设计ch输入与输出操作.ppt(19页珍藏版)》请在三一办公上搜索。

1、第三章 输入与输出操作管理,C语言无I/O语句,I/O操作由函数实现#include,格式:getchar()功能:从键盘读一字符返值:正常,返回读取的代码值;出错,返回EOF(-1),3.1 输入字符 p81字符输入函数,例,/*use of getchar function*/#include main()int c;printf(Enter a character:);c=getchar();printf(%c-hex%xn,c,c);,运行结果:Enter a character:AA-hex41,格式:putchar(c)参数:c为字符常量、变量或表达式功能:把字符c输出到显示器上返

2、值:正常,为显示的代码值;出错,为EOF(-1),3.2 字符输出(p84)字符输出函数,/*ch3.1-1.cpp*/#include main()int c;char a;c=65;a=B;putchar(c);putchar(n);putchar(a);,运行结果:A B,例,例p82 输入字符,判断打印是字母、数字还是其它特殊字符。测试字符是字母或数字的函数有:isalpha(ch)isdigit(ch);这些函数包含在ctype.h。,3.3 格式输入(p85)格式输入函数:,格式:scanf(“格式控制串”,地址表)功能:按指定格式从键盘读入数据,存入地址表指定的 存储单元中,并按

3、回车键结束返值:正常,返回输入数据个数,地址表:变量的地址,常用取地址运算符&格式字符:d,i,o,x,u,c,s,f,e,例 scanf(“%d”,输入:10 则 a=10,例 scanf(“%x”,输入:11 则 a=17,附加格式说明符(修饰符),例 scanf(“%4d%2d%2d”,输入 19991015 则1999yy,10 mm,15 dd,例 scanf(“%3d%*4d%f”,则123k,8765.43f,例 scanf(“%2d%*3d%2d”,输入 1234567 则12a,67b,例 scanf(“%3c%2c”,输入 abcde 则ac1,d c2,输入分隔符的指定一

4、般以空格、TAB或回车键作为分隔符其它字符做分隔符:格式串中两个格式符间字符,例 scanf(“%d%o%x”,输入 123 123 123 输出 a=123,b=83,c=291,例 scanf(“%d:%d:%d”,输入 12:30:45 则12 h,30 m,45 s,例 scanf(“%d,%d”,&a,&b)输入 3,4 则3a,4 b,例 scanf(“a=%d,b=%d,c=%d”,输入 a=12,b=24,c=36,说明:用“%c”格式符时,空格和转义字符作为有效字符输入,如 scanf(“%c%c%c”,若输入abc 则ac1,c2,b c3,输入数据时,遇以下情况认为该数据

5、结束:遇空格、TAB、或回车遇宽度结束遇非法输入,如 scanf(“%d%c%f”,若输入1234a123o.26 则 1234 a,a b,123 c,输入函数留下的“垃圾”:,例 int x;char ch;scanf(“%d”,执行:123输出:x=123,ch=10,例 int x;char ch;scanf(“%d”,执行:123输出:x=123,ch=10,解决方法:(1)用getchar()清除(2)用函数fflush(stdin)清除全部剩余内容 例(3)用格式串中空格或“%*c”来“吃掉”,例 int x;char ch;scanf(“%d”,格式:printf(“格式控制串

6、”,输出表)功能:按指定格式向显示器输出数据返值:正常,返回输出字节数;出错,返回EOF(-1),3.4 格式输出(p94)格式输出函数,输出表:要输出的数据(可以没有,多个时以“,”分隔)格式控制串:包含两种信息格式说明:%修饰符格式字符,用于指定输出格式普通字符或转义序列:原样输出格式字符,int a=567;printf(“%d”,a);,int a=255;printf(“%x”,a);,int a=65;printf(“%o”,a);,int a=567;printf(“%u”,a);,char a=65;printf(“%c”,a);,printf(“%s”,“ABC”);,flo

7、at a=567.789;printf(“%e”,a);,float a=567.789;printf(“%f”,a);,float a=567.789;printf(“%g”,a);,printf(“%”);,567,ff,101,567,A,ABC,5.677890e+02,567.789000,567.789,%,说明格式字符要用小写格式字符与输出项个数应相同,按先后顺序一一对应输出转换:格式字符与输出项类型不一致,自动按指定格式输出,例 main()unsigned int u=65535;printf(”u=%dn,u);输出结果:u=-1,例 int a=3,b=4;printf(

8、“%d%dn”,a,b);printf(“a=%d,b=%dn”,a,b);,例 int a=3,b=4;printf(“%d%dn”,a,b);printf(“a=%d,b=%dn”,a,b);输出结果:3 4 a=3,b=4,附加格式说明符(修饰符),例 int a=1234;float f=123.456;char ch=a;printf(“%8d,%2dn”,a,a);printf(“%f,%8f,%8.1f,%.2f,%.2en”,f,f,f,f,f);printf(“%3cn”,ch);,运行 1234,1234结果:123.456000,123.456000,123.5,123.

9、46,1.23e+02 a,例 static char a=“Hello,world!”printf(“%sn%15sn%10.5sn%2.5sn%.3sn”,a,a,a,a,a);,运行结果:Hello,world!Hello,world!Hello Hello Hel,例 m.n,例 int a=1234;float f=123.456;static char c=“Hello,world!”;printf(“%8d,%-8dn”,a,a);printf(“%10.2f,%-10.1fn”,f,f);printf(“%10.5s,%-10.3sn”,c,c);,运行结果:1234,1234

10、 123.46,123.5 Hello,Hel,例-,例 int a=1234;float f=123.456;printf(“%08dn”,a);printf(“%010.2fn”,f);printf(“%0+8dn”,a);printf(“0+10.2fn”,f);,例 0、+,例 int a=123;float f=123.456;printf(“%o,%#o,%X,%#Xn”,a,a,a,a);printf(“%#fn”,f);,例#o、#x、#X,例 long a=65536;printf(“%d,%8ldn”,a,a);,例 l,/00001234,/0000123.46,/+00

11、01234,/+000123.56,/173,0173,7B,0X7B,/0,65536,3.3 程序举例,/*Calculate Area*/#include#include main()float a,b,c,s,area;scanf(%f,%f,%f,例 输入三角形边长,求面积,输入:3,4,6 输出:a=3.00,b=4.00,c=6.00 s=6.50 area=5.33,例 从键盘输入大写字母,用小写字母输出,/*Upper letter to lower letter*/#include stdio.hmain()char c1,c2;c1=getchar();printf(%c

12、,%dn,c1,c1);c2=c1+32;printf(%c,%dn,c2,c2);,输入:A 输出:A,65 a,97,Code Quantity Rate Value-Total Value-,例pp.103 The ABC Electric Company manufactures four consumer products.Their inventory position on a particular day is given below:,It is required to prepare the inventory report table in the following f

13、ormat:INVENTORY REPORT,例pp.104 Draw a graph to determine the reliability of an electronic component at various operating times,from 0 to 3000 hours.The reliability of an electronic component is given by Reliability(r)=e(-t):the component failure rate per hour.The failure rate(lambda)is 0.001.t:the time of operation in hours.,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号