Introduction to C Standard Input and Output Instructor Yin Lou 02 11 2011 Introduction to C CS 2022 Spring 2011 Lecture 9 stdio h I Each source file that refers to an input output library function must contain the line I include stdio h Introduction to C CS 2022 Spring 2011 Lecture 9 stdio h I Each source file that refers to an input output library function must contain the line I I include stdio h When the name is bracketed by and a search is made for the header in a standard set of places I On Unix systems typically in the directory usr include Introduction to C CS 2022 Spring 2011 Lecture 9 Example include stdio h include ctype h int main int c while c getchar EOF putchar tolower c return 0 Introduction to C CS 2022 Spring 2011 Lecture 9 Formatted Output printf int printf char format arg1 arg2 Introduction to C CS 2022 Spring 2011 Lecture 9 Formatted Output printf int printf char format arg1 arg2 I printf converts formats and prints its arguments on the standard output under control of the format I It returns the number of characters printed I printf x d y d n x y Introduction to C CS 2022 Spring 2011 Lecture 9 Basic printf Conversions I d i int decimal number I o int unsigned octal number without a leading zero I x X int unsigned hexadecimal number without a leading 0x or 0X using abcdef or ABCDEF for 10 15 I u int unsigned decimal number I c int single character I s char print characters from the string until a 0 or the number of characters given by the precision I f double m dddddd where the number of d s is given by the precision default 6 Introduction to C CS 2022 Spring 2011 Lecture 9 What Can Go Wrong printf s Introduction to C CS 2022 Spring 2011 Lecture 9 What Can Go Wrong printf s char s My name is s printf s equivalent to printf My name is s printf s s safe Introduction to C CS 2022 Spring 2011 Lecture 9 Print to String int sprintf char string char format arg1 arg2 Introduction to C CS 2022 Spring 2011 Lecture 9 Print to String int sprintf char string char format arg1 arg2 I sprintf formats the arguments in arg1 arg2 etc according to format as before but places the result in string instead of the standard output I string must be big enough to receive the result I sprintf s x d y d n x y Introduction to C CS 2022 Spring 2011 Lecture 9 Formatted Input scanf int scanf char format Introduction to C CS 2022 Spring 2011 Lecture 9 Formatted Input scanf int scanf char format I I scanf reads characters from the standard input interprets them according to the specification in format and stores the results through the remaining arguments Each of the other argument must be a pointer Introduction to C CS 2022 Spring 2011 Lecture 9 Formatted Input scanf int scanf char format I I I I I I scanf reads characters from the standard input interprets them according to the specification in format and stores the results through the remaining arguments Each of the other argument must be a pointer It returns as its value the number of successfully matched and assigned input items On the end of file EOF is returned Note that this is different from 0 which means that the next input character does not match the first specification in the format string The next call to scanf resumes searching immediately after the last character already converted scanf d x scanf s name char name 1000 Introduction to C CS 2022 Spring 2011 Lecture 9 Read from a String int sscanf char string char format arg1 arg2 Introduction to C CS 2022 Spring 2011 Lecture 9 Read from a String int sscanf char string char format arg1 arg2 The formate string main contain I Blank or tabs which are not ignored I Ordinary characters I Conversion specifications I sscanf x 5 y 6 x d y d x y Introduction to C CS 2022 Spring 2011 Lecture 9 Example include stdio h int main double sum v while scanf lf v 1 printf t 2f n sum v return 0 Introduction to C CS 2022 Spring 2011 Lecture 9 Error Handling stderr and exit I There is a second output stream called stderr I Output written on stderr normally appears on the screen even if the standard output is redirected I exit int signal is function to terminate the program with a signal specified 0 signals that all is well non zero values usually signal abnormal situations Introduction to C CS 2022 Spring 2011 Lecture 9 Error Handling stderr and exit I There is a second output stream called stderr I Output written on stderr normally appears on the screen even if the standard output is redirected I exit int signal is function to terminate the program with a signal specified 0 signals that all is well non zero values usually signal abnormal situations include stdio h include stdlib h main char p char malloc 50 sizeof char if p NULL fprintf stderr Not enough memory n exit 1 do something exit 0 Introduction to C CS 2022 Spring 2011 Lecture 9
View Full Document
Unlocking...