850 likes | 2.38k Views
Chapter 4 Managing Input and Output Operations. PROGRAMMING IN C. Chapter 4. C hasn’t 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( )
E N D
Chapter 4Managing Input and Output Operations PROGRAMMING IN C
Chapter 4 • C hasn’t 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( ) # include <stdio.h># include "stdio.h" standard input and output
getchar( ) & putchar( ) • Form: variable = getchar(); putchar(character); #include <stdio.h> main() { char c ; c = getchar ( ); putchar ( c ); putchar ( getchar () ); printf ( "%c", getchar () ); putchar ('D'); } #include <stdio.h> main() { char c ; c = getchar ( ); putchar ( c ); putchar ( getchar () ); printf ( "%c", getchar () ); putchar ('D'); printf("%d", getchar()); } abc a b c D 10
printf( ) – Formatted Output • Form: printf("control string", arg1, …, argn); e.g. printf("price=$%.2f\n, amount=%d.", pr*am, am); • Control string • The characters that will be outputted as they appear. e.g. price amount = $ , . • Escape sequence characters. e.g. \n • Format specifications. e.g. %f, %d
printf( ) – Formatted Output • Form: printf("control string", arg1, …, argn); e.g. printf("price=$%.2f\n, 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 comma. • 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
printf( ) – Formatted Output printf ("%f",123.45); printf ("%c, %c",65, 'A'); printf ("%u, %U",-3, 'A'); printf ("%e",12.3); printf ("%s","Hello!"); printf ("%x, %X",-3, 'A'); printf ("%d, %i",-3, 'A'); printf ("%g",123.450); printf ("%%"); printf ("%o, %O",-3, 'A'); • Format specifications Output: 123.450000 Output: 123.45 Output: 1.230000e+01 Output: A, A Output: Hello! Output: % Output: 65533, %U Output: 177775, %O Output: fffd, 41 Output: -3, 65
printf( ) – Formatted Output • Accessorial format specifications printf ("%3d", 12); printf ("%3f", 12.3); printf ("%.1f", 12.36); printf ("%.1f, %.1f", 1.25, 1.251); printf ("%4.2s", "abc"); printf ("%ld, %d", 65536, 65536); printf ("%2c, %-2c", 'A', 'A'); Output: 12 Output: 12.300000 Output: 12.4 Output: 1.2, 1.3 Output: ab Output: 65536, 0 Output: A, A
scanf( ) – Formatted Input • Form: scanf ("control string", arg1, …, argn);e.g. scanf("%d,%c", &num, &ch); • format specifications %d, %i, %o, %x, %u, %c, %s, %f, %e, %gthe same as those in printf function.
scanf( ) – Formatted Input e.g. scanf("%d%*d%d", &a, &b); input:1234567 result:12a, 67b e.g. scanf("%3d%*2d%f", &i, &f); input:123456.789 result: 123i, 6.789f • Form: scanf ("control string", arg1, …, argn);e.g. scanf("%d,%c", &num, &ch); • Accessorial format specifications e.g. scanf("%3d%f", &i, &f); input:12345.6789 result: 123i, 45.6789f
scanf( ) – Formatted Input e.g. scanf("%d%d%f", &n1, &n2, &f); input:1234567.89 result:12n1, 345n2, 67.89 f • Form: scanf ("control string", arg1, …, argn);e.g. scanf("%d,%c", &num, &ch); • Separators • When it read in the integers or real numbers, it defaults blank spaces, tabs or newlines as separators.
scanf( ) – Formatted Input e.g. scanf("%d, %d", &a, &b); input:12, 345 result:12a, 345b input: 12345 result:12a, nothingb e.g. scanf("a=%d, b=%d", &a, &b); input:a=12, b=345 result:12a, 345b input: 12,345 result: nothinga, nothingb • Form: scanf ("control string", arg1, …, argn);e.g. scanf("%d,%c", &num, &ch); • Separators • It also can specify separators, that is those characters except format specifications.In this way, the input must accord with the specified format, otherwise the input most probably is read in by errors.
scanf( ) – Formatted Input • Form: scanf ("control string", arg1, …, argn);e.g. scanf("%d,%c", &num, &ch); • 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", &i, &ch); input:12a result:12i, '' ch
scanf( ) – Formatted Input e.g. scanf("%d%d%f", &n1, &n2, &f); input:1234567.89 result:12n1, 345n2, 67.89 f • Form: scanf ("control string", arg1, …, argn);e.g. scanf("%d,%c", &num, &ch); • 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.
scanf( ) – Formatted Input e.g. scanf("%3d%f", &i, &f); input:12345.6789 result: 123i, 45.6789f e.g. scanf("%d%c%f", &a, &b, &c); input:123a456o.78 result: 123a, 'a'b, 456.0c • Form: scanf ("control string", arg1, …, argn);e.g. scanf("%d,%c", &num, &ch); • 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.
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 = x;) y -> x (x = y;) temp -> y (y = temp;) • Step4: Output the value of x and y.
Program 1 Please input x and y: 4 5.6 Before exchange: x=4.00, y=5.60 After exchange: x=5.60, y=4.00 main() { float x, y, temp; printf ("Please input x and y:\n") ; scanf ( "%f%f", &x, &y ); printf ("Before exchange: x=%.2f, y=%.2f\n", x, y); temp = x ; x = y; y = temp; printf ("After exchange: x=%.2f, y=%.2f", x, y); }
Program 2 • Read in a lowercase letter, convert it into 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 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 #include <stdio.h> main() { char ch; printf("Please input a lowercase:") ; ch = getchar(); printf("The lowercase is %c, and its ASCII value is %d\n", ch, ch); ch = ch - 32; printf("The uppercase equivalent is:%c, and its ASCII value is %d\n", ch, ch); }
Homework • Review Questions • Programming Exercises