资源描述
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*/,
展开阅读全文