InputandOutputinC输入和输出C

上传人:ra****d 文档编号:252426598 上传时间:2024-11-15 格式:PPT 页数:12 大小:42KB
返回 下载 相关 举报
InputandOutputinC输入和输出C_第1页
第1页 / 共12页
InputandOutputinC输入和输出C_第2页
第2页 / 共12页
InputandOutputinC输入和输出C_第3页
第3页 / 共12页
点击查看更多>>
资源描述
Click Here to Add Title,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,C Programming,Lecture 3,:I/O in C,printf()scanf(),Input/Output in C,C has no built-in statements for input or output.,A library of functions is supplied to perform these operations.The I/O library functions are listed the“header file.,You do not need to memorize them,just be familiar with them.,Streams,All input and output is performed with streams.,A stream is a sequence of characters organized into lines.,Each line consists of zero or more characters and ends with the newline character.,ANSI C standards specify that the system must support lines that are at least 254 characters in length(including the newline character).,Types of Streams in C,Standard input stream is called stdin and is normally connected to the keyboard,Standard output stream is called stdout and is normally connected to the display screen.,Standard error stream is called stderr and is also normally connected to the screen.,Formatted Output with printf,The printf()function prints output to stdout,according to format and other arguments passed to printf().The syntax is:,printf(“format,var1,var2,);,The string format consists of two types of items-characters that will be printed to the screen,and format commands that define how the other arguments to printf()are displayed.Basically,you specify a format string that has text in it,as well as special characters that map to the other arguments of printf().For example:,char name20=Bob;int age=21;printf(Hello%s,you are%d years oldn,name,age);,Hello Bob,you are 21 years old,Formatted Output with printf,Format Conversion Specifiers:,d-displays a decimal(base 10)integer,l-used with other specifiers to indicate a long,e-displays a floating point value in exponential notation,f-displays a floating point value,g-displays a number in either e or f format,c-displays a single character,s-displays a string of characters,i,-,signed integer,u-unsigned integer,x-unsigned hexadecimal,with lowercase letters,o-octal,%-a%sign,Formatted input with scanf();,The scanf()function reads input from stdin,according to the given format,and stores the data in the other arguments.The syntax is:,scanf(“format,It works a lot like printf().The format string consists of control characters,whitespace characters,and non-whitespace characters.,Example:,float a;int b;,scanf(“%f%d,scanf()reads the input,matching the characters from,format,.When a control character is read,it puts the value in the next variable.Whitespace(tabs,spaces,etc)are skipped.Non-whitespace characters are matched to the input,then discarded.If a number comes between the%sign and the control character,then only that many characters will be converted into the variable.If scanf()encounters a set of characters,denoted by the%control character,then any characters found within the brackets are read into the variable.The return value of scanf()is the number of variables that were successfully assigned values,or,EOF,if there is an error.,Input/Output in C,getchar();,This function provides for getting exactly one character from the keyboard.,Example:,char ch;,ch=getchar();,Input/Output in C,putchar(char);,This function provides for printing exactly one character to the screen.,Example:,char ch;,ch=getchar();/*input a character from kbd*/,putchar(ch);/*display it on the screen*/,Input/Output in C,getc(*file);,This function is similar to getchar()except the input can be from the keyboard or a file.,Example:,char ch;,ch=getc(stdin);/*input from keyboard*/,ch=getc(fileptr);/*input from a file*/,Input/Output in C,putc(char,*file);,This function is similar to putchar()except the output can be to the screen or a file.,Example:,char ch;,ch=getc(stdin);/*input from keyboard*/,putc(ch,stdout);/*output to the screen*/,putc(ch,outfileptr);/*output to a file*/,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业管理 > 商业计划


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

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


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