Chapter 4 输入输出_第1页
Chapter 4 输入输出_第2页
Chapter 4 输入输出_第3页
Chapter 4 输入输出_第4页
Chapter 4 输入输出_第5页
已阅读5页,还剩39页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include main()char answer;printf(“Would you like to know my name?n”)printf(“Type Y for Yes and N for No:”);answer = getchar();if(answer = Y| answer = y) printf(“nnMy name is BUSE BEEn”);else printf(“nnYou are good for nothingn”);Output Would you like to know my name? Type Y for YES and N for NO: Y

2、My name is BUSY BEE Would you like to know my name? Type Y for YES and N for NO:n You are good for nothingNote: We shall be using decision statements like if, ifelse and while extensively in this chapter. They are discussed in detail in Chapters 5 and 6.#include #include main() char character; print

3、f(“Press any keyn”) character = getchar(); if(isalpha(character) 0) printf(“The character is a letter.”); else if(isdigit(character)0) printf(“The character is a digit.”) else printf(“The character is not alphanumeric.”);Output Press any key h The character is a letter. Press any key 5 The character

4、 is a digit. Press any key * The character is not alphanumeric#include #include #include main() char alphabet; printf(“Enter an alphabetn”); putchar(“n”); /*move to next line*/ alphabet = getchar(); if(islower(alphabet) putchar(toupper(alphabet); else putchar(tolower(alphabet);等价于?等价于?若不用若不用ctype.h实

5、现,可以?实现,可以?Output Enter an alphabet a A Enter an alphabet Q q Enter an alphabet z Z Control string (also known as format string) contains field specifications, which direct the interpretation of input data.Example: Format outputmain() int m = 12345; long n = 987654; printf(“%dn”,m); printf(“%10dn”,m

6、); printf(“%010dn”,m); printf(“-10%dn”,m); printf(“%10ldn”,n); printf(“%10ldn”,-n);Output 12345 12345 0000012345 12345 987654 -987654Example: y=98.7654 Format output了解了解main() float y=98.7654; printf(“%7.4fn”,y);printf(“%fn”,y);printf(“%7.2fn”,y);printf(“%-7.2fn”,y);printf(“%07.2fn”,y);printf(“%*.*f

7、n”,7,2,y);printf(“n”);printf(“%10.2en”,y);printf(“%12.4en”,-y);printf(“%-10.2en”,y); printf(“%en”,y);Output 98.7654 98.765404 98.77 98.77 0098.77 98.77 9.88e+001 -9.8765e+001 9.88e+001 9.876540e+001printf(“%c”, a);printf(“%c”, n);printf(“%c”,);printf(“%c”, 0 x50);N EWDELH l110001N EWD ELH l110001N E

8、WD ELH lN EWDN EWDELH LN EWDELH l110001main() char x=A; char name20=“ANIL KUMAR GUPTA”; printf(“OUTPUT OF CHARACTERSnn”);printf(“%cn%3cn%5cn”,x,x,x);printf(“%3cn%cn”,x,x);printf(“n”);printf(“OUTPUT OF STRINGSnn”);printf(“%sn”,name);printf(“%20sn”,name);printf(“%20.10sn”,name);printf(“%.5sn”,name);pr

9、intf(“%-20.10sn”,name); printf(“%5sn”,name);Output OUTPUT OF CHARACTERS A A A A A OUTPUT OF STRINGS ANIL KUMAR GUPTA ANIL KUMAR GUPTA ANIL KUMAR ANIL ANIL KUMAR ANIL KUMAR GUPTA示例示例了解了解main() int a,b,c,x,y,z; int p,q,r; printf(“Enter three integer numbersn”); scanf(“%d %*d %d”,&a,&b,&c);

10、 printf(“%d %d %d nn”,a,b,c); printf(“Enter two 4-digit numbersn”); scanf(“%2d %4d”,&x,&y); printf(“%d %dnn”,x,y); printf(“Enter two integersn”); scanf(“%d %d”,&a,&x); printf(“%d %dnn”,a,x); printf(“Enter a nine digit numbern”); scanf(“%3d %4d %3d”,&p,&q,&r); printf(“%d %

11、d %d nn”,p,q,r); printf(“Enter two three digit numbersn”); scanf(“%d %d”,&x,&y); printf(“%d %d”,x,y);Output Enter three integer numbers 1 2 3 1 3 -3577 Enter two 4-digit numbers 6789 4321 67 89 Enter two integers 44 66 4321 44 Enter a nine-digit number 123456789 66 1234 567 Enter two three-d

12、igit numbers 123 456 89 123 main() float x,y; double p,q; printf(“Values of x and y:”); scanf(“%f %e”,&x,&y); printf(“n”); printf(“x = %fny = %fnn”,x,y); printf(“Values of p and q:”); scanf(“%lf %lf”,&p,&q); printf(“nnp = %.12lfnq = %.12e”,p,q);Output Values of x and y:12.3456 17.5e-

13、2 x = 12.345600 y = 0.175000 Values of p and q: 4.142857142857 18.5678901234567890 p = 4.142857142857 q = 1.856789012346e+001main() int no; char name115,name215,name315; printf(“Enter serial number and name onen”); scanf(“%d %15c”,&no,name1); printf(“%d %15snn”,no,name1); printf(“Enter serial nu

14、mber and name twon”); scanf(“%d %s”,&no,name2); printf(“%d %15snn”,no,name2); printf(“Enter serial number and name threen”); scanf(“%d %15s”,&no,name3); printf(“%d %15snn”,no,name3)Output Enter serial number and name one 1 123456789012345 1 123456789012345r Enter serial number and name two 2

15、 New York 2 New Enter serial number and name three 2 York Enter serial number and name one 1 123456789012 1 123456789012r Enter serial number and name two 2 New-York 2 New-York Enter serial number and name three 3 London 3 Londonmain() char address80; printf(“Enter addressn”); scanf(“% a-z”,address)

16、; printf(“%-80snn”,address);Output Enter address new delphi 110002 new delphi main() char address80; printf(“Enter addressn”); scanf(“%n”,address); /可读空格可读空格 printf(“%-80snn”,address);Output Enter address new Delphi 110 002 new Delphi 110 002main() int a; float b; char c; printf(“Enter values of a,b

17、 and cn”); if(scanf(“%d %f %c”,&a,&b,&c)=3) printf(“a=%d b=%f c=%cn”,a,b,c); else printf(“Error in input.n”);Output Enter values of a,b and c 12 3.45 A a=12 b=3.450000 c=A Enter values of a,b and c 23 78 9 a=23 b=78.00000 c=9 Enter values of a,b and c 8 A 5.25 Error in input Enter values of a,b and c Y 12 67 Error in input Enter values of a,b and c 15.75 23 X a=15 b=

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论