c语言程序设计课件ppt(英文)c program language 之3 inpu and output_第1页
c语言程序设计课件ppt(英文)c program language 之3 inpu and output_第2页
c语言程序设计课件ppt(英文)c program language 之3 inpu and output_第3页
c语言程序设计课件ppt(英文)c program language 之3 inpu and output_第4页
c语言程序设计课件ppt(英文)c program language 之3 inpu and output_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

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

文档简介

Chapter TInpu and OutputContents1Data output2Data input3Examples4Control flow-sequenceControl flow-sequenceABNotes in sequence structure Declaration statements first ,then execution statements; Execution statements review: Assignment statement Compond statement expression statementInput /output function call statementInitialization : assign value while declaration;main( ) int a;a=5;main( ) int a=5;Data outputv Character input and outputv Formatted input and outputYour programs will need to include the standard I/O header file so do: #include Input and output are not part of the C There are a set of functions that provide input and output in standard library.NoteCharacter outputputchar(char variable)Formatoutputs a single character to screen.Role Only single characterResult #includeData output#includemain( ) char c=a;putchar( c);Examplemain( ) char c1, c2,c3;c1=a;c2=b;c3=c;putchar(c1);putchar(c2);putchar(c3); How many characters will be displayed ?How about their arrangement? Data outputData outputResultFormatRole NoteFormatted outputprintf ( control string, argument list )Output the variables value in the given format on screenNo argument, only print out control string.printf(“Hello!”);Arguments including constants, variables, mathematical expressionsData outputtype conversion specifier. Control string:Format Output% d output int value in decimal form % o output int value in octal form % x output int value in hexadecimal form% ld: output long int value in decimal form% u output unsign int value in decimal form% f output float value in decimal form% e output float value in exponent formData outputFormat Output% c Output one character% s Output a string% g Output the shorter one between %e and %f% md the output width is m , flush right% -md the output width is m , flush left% m.nf the output width is m , decimal part is n , flush right% -m.nf the output width is m , decimal part is n , flush lefttype conversion specifier printf uses its first argument to decide how many arguments follow and what their type is . It will get confused ,and you will get wrong answers, if there are not enough arguments of if they are the wrong type. Data outputprintf(“%d,%d n”,a,b); if: a=5,b=8a=5,b= 8 printf(“a=%d,b=%dn”,a,b); 5, 8 printf(“%5d%5dn”,a,b); 5 8printf(“Turbo C”); Turbo Cprintf(“%s ”, “Turbo C”); Turbo Cmain() int a=16; char e =A; unsigned b; long c; float d;b=65535; c=123456; d=123.45;printf(“a=%d,%4d,%- 6d,c=%dn”,a,a,a,c);printf(“%o,%x,%u,%dn”,b,b,b,b );printf(“%f,%e,%13.3e,%gn”,d,d,d,d);printf(“%c,%s,%7.3sn”,e,” China”,”Beijing”);a=16, 16,16 , c= - 7616177777, ffff ,65535 , -1123.450000,1.234500e+002, 1.235e+002,123.45A, China, BeiData outputData outputFormat 2 Format 3Format 1 Character input functions and formatted input function1、 character input Inputs a single character from keyboard.Input ,no display ,no Entergetch( )Input and display ,no Entergetche( )After inputing ,one should press Enter keygetchar( )main() int a=3,b=5;printf(“%d,%d,%dn”,a,b, a+b);getch();Data input main( ) char c;c=getchar( );putchar(c); Output the character what you input 2 formatted inputscanf(control string, arguments list)Format:scanf reads characters from the standard input, interprets them according to the specification in format and stores the results through the remaining arguments.Result: the arguments list must be address , indicate where the corresponding converted input should be stored.Note:int a,b;scanf(“%d,%d”, Data input Discussion Format control string similar to printf The arguments are ADDRESS of variables If the inputs dont match, the type conversion specifier, the scanf will stop reading the inputs,return an errorscanf will terminate entry of string when it reads the first space or new line.If the input string length is larger than 3,The first 3 chars are assigned to address others are K;scanf(“%3d”,input: 123456789 K is 123main() int a; float b,c;scanf(“%2d%3f%4f”,printf(“a=%d,b=%f,c=%fn”,a,b,c);input: 12345678987654321output?a=12,b=345.000000,c=6789.00000012 345 6789Data input If additional chars are inserted in control string, they must appear in the input a,b;scanf(“%d%d”, scanf(“%d ,%d”, 3 , 5 scanf(“a =%d,b=%d”, Input: a=3,b=5 char c1,c2,c3; scanf(“%c%c%c ”, A B

温馨提示

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

评论

0/150

提交评论