《programming in ANSI C-Chapter 4 Managing input and output operations.ppt》由会员分享,可在线阅读,更多相关《programming in ANSI C-Chapter 4 Managing input and output operations.ppt(19页珍藏版)》请在三一文库上搜索。
1、Chapter 4Managing Input and Output Operations,PROGRAMMING IN ANSI C,Chapter 4,C hasnt any built-in input/output statements as part of its syntax. All of input/output operations are carried out through standard input/output functions. e.g. printf( ) scanf( )getchar( )gets( )printf( )putchar( )puts( )
2、,# include # include stdio.h,standard input and output,getchar( ) putchar(character);,#include main() char c ; c = getchar ( ); putchar ( c ); putchar ( getchar () ); printf ( %c, getchar () ); putchar (D); ,abc,a,b,c,D,#include main() char c ; c = getchar ( ); putchar ( c ); putchar ( getchar () );
3、 printf ( %c, getchar () ); putchar (D); printf(%d, getchar(); ,10,printf( ) Formatted Output,Form:printf(control string, arg1, , argn); e.g. printf(price=$%.2fn, amount=%d., pr*am, am); Control string The characters that will be outputted as they appear. e.g. price amount = $ , . Escape sequence ch
4、aracters.e.g. n Format specifications.e.g. %f, %d,printf( ) Formatted Output,Form:printf(control string, arg1, , argn); e.g. printf(price=$%.2fn, amount=%d., pr*am, am); The outputted expression list: (arg1, , argn) There can be no any expression or more than one expression. They are separated by co
5、mma. In spite of what type these expressions are, they are always outputted in specified format.,printf(%c, %d, 97, a); Output: a, 97,printf(%d, %u, 32767+1, 32767+1); Output: -32768, 32768,Output:-3, 65,Output:fffd, 41,printf (%d, %i,-3, A);,printf (%x, %X,-3, A);,printf( ) Formatted Output,Format
6、specifications,Output:177775, %O,printf (%o, %O,-3, A);,Output:65533, %U,printf (%u, %U,-3, A);,Output:A, A,printf (%c, %c,65, A);,Output:Hello!,printf (%s,Hello!);,Output:123.450000,printf (%f,123.45);,Output: 1.230000e+01,printf (%e,12.3);,Output: 123.45,printf (%g,123.450);,Output: %,printf (%);,
7、Output: 12,printf (%3d, 12);,Output: 12.300000,printf (%3f, 12.3);,Output: 12.4,printf (%.1f, 12.36);,Output: 1.2, 1.3,printf (%.1f, %.1f, 1.25, 1.251);,Output: ab,printf (%4.2s, abc);,Output: 65536, 0,printf (%ld, %d, 65536, 65536);,Output: A, A,printf (%2c, %-2c, A, A);,printf( ) Formatted Output,
8、Accessorial format specifications,scanf( ) Formatted Input,Form:scanf (control string, arg1, , argn);e.g. scanf(%d,%c, format specifications %d, %i, %o, %x, %u, %c, %s, %f, %e, %gthe same as those in printf function.,scanf( ) Formatted Input,Form:scanf (control string, arg1, , argn);e.g. scanf(%d,%c
9、, Accessorial format specifications,e.g.scanf(%3d%f, input: 12345.6789 result:123i, 45.6789f,e.g. scanf(%d%*d%d, input: 123456.789 result:123i, 6.789f,scanf( ) Formatted Input,Form:scanf (control string, arg1, , argn);e.g. scanf(%d,%c, Separators When it read in the integers or real numbers, it defa
10、ults blank spaces, tabs or newlines as separators.,e.g. scanf(%d%d%f, input: 12345 67.89 result: 12n1, 345n2, 67.89 f,scanf( ) Formatted Input,Form:scanf (control string, arg1, , argn);e.g. scanf(%d,%c, Separators It also can specify separators, that is those characters except format specifications.
11、In this way, the input must accord with the specified format, otherwise the input most probably is read in by errors.,e.g. scanf(%d, %d, input: 12, 345 result: 12a, 345binput: 12345 result: 12a, nothingb,e.g. scanf(a=%d, b=%d, input: a=12, b=345 result: 12a, 345binput:12, 345 result: nothinga, nothi
12、ngb,scanf( ) Formatted Input,Form:scanf (control string, arg1, , argn);e.g. scanf(%d,%c, Separators When it read in a character, the blank space, tab and newline character are no longer separators, but as a valid character.,e.g. scanf(%d%c, input: 12a result: 12i, ch,scanf( ) Formatted Input,Form:sc
13、anf (control string, arg1, , argn);e.g. scanf(%d,%c, How to judge a data is finished? If “scanf” read an integer of real number, when it encounters a blank space, or tab, or newline character, it considers the number is finished.,e.g. scanf(%d%d%f, input: 12345 67.89 result: 12n1, 345n2, 67.89 f,sca
14、nf( ) Formatted Input,Form:scanf (control string, arg1, , argn);e.g. scanf(%d,%c, How to judge a data is finished? When the input data achieves the specified field wide, “scanf” considers the data is finished. When “scanf” encounters an illegal character, it considers the data is finished.,e.g.scanf
15、(%3d%f, input: 12345.6789 result:123i, 45.6789f,e.g. scanf(%d%c%f, input: 123a456o.78 result:123a, ab, 456.0c,Program 1,Read in 2 numbers, exchange them, then output them. Step1: Declare 3 float variables x, y and temp. Step2: Read 2 real numbers into x and y. Step3: Exchange them: x - temp (temp =
16、x;)y - x (x = y;) temp - y (y = temp;) Step4: Output the value of x and y.,Program 1,main() float x, y, temp; printf (Please input x and y:n) ; scanf ( %f%f, ,Please input x and y: 4 5.6 Before exchange: x=4.00, y=5.60 After exchange: x=5.60, y=4.00,Program 2,Read in a lowercase letter, convert it i
17、nto its uppercase equivalent, and then output the uppercase and its ASCII value. Step1: Declare a char type variable ch. Step2: Read in a lowercase letter into ch. Step3: Convert it into its uppercase equivalent. ch = ch 32; or ch = ch (a - A); Step4: Output ch and its ASCII value.,Program 2,#includ
18、e main() char ch; printf(Please input a lowercase:) ; ch = getchar(); printf(The lowercase is %c, and its ASCII value is %dn, ch, ch); ch = ch - 32; printf(The uppercase equivalent is:%c, and its ASCII value is %dn, ch, ch); ,Please input a lowercase: The lowercase is m, and its ASCII value is 109 The uppercase equivalent is M, and its ASCII value is 77,m ,Homework,Review Questions P106 4.1, 4.2, 4.4, 4.5 (Write down in your exercise book) Programming Exercises,