Inputamp;Output输入和输出;

上传人:ra****d 文档编号:252414538 上传时间:2024-11-15 格式:PPT 页数:27 大小:127KB
收藏 版权申诉 举报 下载
Inputamp;Output输入和输出;_第1页
第1页 / 共27页
Inputamp;Output输入和输出;_第2页
第2页 / 共27页
Inputamp;Output输入和输出;_第3页
第3页 / 共27页
资源描述:

《Inputamp;Output输入和输出;》由会员分享,可在线阅读,更多相关《Inputamp;Output输入和输出;(27页珍藏版)》请在装配图网上搜索。

1、Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,E-,*,University of WashingtonComputer Programming I,Lecture 5:,Input and Output(I/O),2000 UW CSE,1,Overview,Topics,Output:printf,Input:scanf,Basic format codes,More on initializing var

2、iables,2,Writing Useful Programs,Its hard to write useful programs using only variables and assignment statements,Even our Fahrenheit to Celsius program needed more:,Needed a way to get data into and out of the program,Well learn more about doing this today,Lots of terminology and messy details,but

3、worthwhile.,3,Central,Processing,Unit,Main,Memory,Monitor,Network,Disk(Files),Keyboard,mouse,Whats a Computer?,4,Input:movement of data into memory from outside world(e.g.,from keyboard).,Changes the value of a variable,“read operation,Output:movement of data from memory to outside world(e.g.,to mon

4、itor),“write operation,Does not change value of memory,Basic Definitions,5,Text Output,6,printf(Enter a Fahrenheit temperature:);,scanf(%lf,celsius=(fahrenheit-32.0)*5.0/9.0;,printf(That equals%f degrees Celsius.,celsius);,I/O Statements from a Familiar Program,7,The functions printf and scanf provi

5、de basic display I/O services.,printf(“control string,list of expressions);,scanf(“control string,list of,Control string gives the format of output or input.,Expressions are what to output.,Variables are where to store the input.,&is magic(that is REQUIRED for scanf!),Display Input and Output,8,int

6、numPushups;numPushups=5;printf(“Hello.Do%d pushups.n,numPushups);output:Hello.Do 5 pushups.,%d is a placeholder(“conversion character)for an int value.,n is an escape sequence for“newline character.,printf():Display Output,9,intnumPushups;,numPushups=5;printf(“Hello.);printf(“Do%d pushups.n,numPushu

7、ps);printf(“Do them now.n);output:Hello.Do 5 pushups.Do them now.,What Does the n Do?,10,printf(“control string,list of expressions);,printf might have more than one expression in its list:,printf(“%d times%f is%f.n,multiplier,pi,(double)multiplier*pi),Getting a Little Fancier,11,%placeholders in fo

8、rmat string match expressions in output list in number,order,and type.,int multiplier;double pi;,pi=3.14;multiplier=2;,printf(“%d times%f is%f.n,multiplier,pi,(double)multiplier*pi);,Output:2 times 3.14000 is 6.28000.,Multiple Output Expressions,12,Advanced Output Formatting,This is only the beginni

9、ng!A few of many other things you can do:,Control number of decimals,3.1 vs 3.100000,Exponential(scientific)or decimal notation,3.1 vs 3.1E0,Control total width(including spaces),_3.1 vs _3.1,How?,13,Advanced Output Formatting,This is only the beginning!A few of many other things you can do:,Control

10、 number of decimals,3.1 vs 3.100000,Exponential(scientific)or decimal notation,3.1 vs 3.1E0,Control total width(including spaces),_3.1 vs _3.1,How?,Look in textbook or a reference manual,or online help!,14,Output Format Examples,%10.2f _ _ _ _ 1 2 3.5 5 double,%10.4f _ _ 1 2 3.5 5 0 0,%.2f 1 2 3.5 5

11、,%10d _ _ _ _ _ _ _ 4 7 5 int,%-10d 4 7 5 _ _ _ _ _ _ _,%10c _ _ _ _ _ _ _ _ _ a char,15,scanf(“control string,int numPushups;printf(“Hello.Do how many pushups?);scanf(“%d ,output:Hello.Do how many pushups?5Do 5 pushups.,input list variables MUST be preceded by an&.,input list variables MUST be prec

12、eded by an&.,scanf():Read Input,16,If You Forget the&,The program will compile,but when you execute.,17,If You Forget the&,The program will compile,but when you execute.,18,space(),tab(t),newline(n)are“whitespace,Whitespace is skipped by scanf for int(“%d),and double(“%lf),This means the user can ty

13、pe spaces before a number and they are ignored,Not skipped for char input“%c,each character typed,including spaces,is used,Whitespace,19,Basic rule:,%placeholders in the format must match variables in the input list,MUST,!match one-for-one in,number,order,and type,.,intstudentID;doublegrade;,scanf(“

14、%d%lf,Multiple Inputs,20,Typescanf()printf()char,%c%c,int,%d%d,%i,also works,double,%,lf,%,f,(,l,ong),f,loat,What happens if types dont match?,printf -,garbled output,scanf -,unpredictable errors and dont forget the&!,Format Items Summary,21,Output:printf(“control string,output list);,output list ex

15、pressions;values to be printed,control string types and desired format,for now,NO“&,ever!,Input:scanf(“control string,input list variables;values to be read,control string types and expected format,can be a way of initializing variables,for now,YES“&,always!,Both:%xs,I/O list match in number,order,t

16、ype,printf/scanf Summary,22,Input is the movement of data into memory,In C,we use scanf for input from the keyboard,Output is the movement of data from memory,In C,use printf for output to the screen,Know the basic printf/scanf rules,and know them well,Be aware that advanced formatting options exist and can be looked up when needed,I/O Summary,23,Bonus Topic:More on Initializing Variables,Review:,Initialization,means giving something a value for the,first,time.,Potential ways to initialize:,Assi

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

相关资源

更多
正为您匹配相似的精品文档
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!