# include "stdio.h" standard input and output"> # include "stdio.h" standard input and output">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Managing Input and Output Operations

Similar presentations


Presentation on theme: "Chapter 4 Managing Input and Output Operations"— Presentation transcript:

1 Chapter 4 Managing Input and Output Operations
PROGRAMMING IN ANSI C

2 # include <stdio.h> # include "stdio.h"
2/22/2019 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

3 2/22/2019 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

4 printf( ) – Formatted Output
2/22/2019 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

5 printf( ) – Formatted Output
2/22/2019 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", , ); Output: , 32768

6 printf( ) – Formatted Output
2/22/2019 printf( ) – Formatted Output printf ("%c, %c",65, 'A'); printf ("%s","Hello!"); printf ("%f",123.45); printf ("%%"); printf ("%g", ); printf ("%e",12.3); printf ("%o, %O",-3, 'A'); printf ("%u, %U",-3, 'A'); printf ("%x, %X",-3, 'A'); printf ("%d, %i",-3, 'A'); Format specifications Output: -3, 65 Output: % Output: Output: fffd, 41 Output: e+01 Output: Output: , %O Output: A, A Output: Hello! Output: 65533, %U 1 %d, %i Signed decimal integer 2 %x, %X Unsigned hexadecimal integer (without leading 0x) 3 %o Unsigned octal integer (without leading 0) 4 %u Unsigned decimal integer 5 %c Single character 6 %s Sting 7 %f Real number in decimal notation 8 %e, %E Real number in exponential notation 9 %g, %G Real number either f-type or e-type depending on the length of the value without insignificant zero 10 %% %

7 printf( ) – Formatted Output
2/22/2019 printf( ) – Formatted Output Accessorial format specifications 1 w Specifies the minimum field width for the output. 2 .p To real number, specifies the number of digits after the decimal point (rounded). To string, instructs the first p characters to be outputted. To integer or single character, it is invalid. 3 l Precede d, i, o, x, u to outputs long type integer. Precede f, e, g to outputs double type real number. 4 - Left-justified, and remaining field will be blank. printf ("%4.2s", "abc"); printf ("%ld, %d", 65536, 65536); printf ("%2c, %-2c", 'A', 'A'); printf ("%.1f, %.1f", 1.25, 1.251); printf ("%3d", 12); printf ("%3f", 12.3); printf ("%.1f", 12.36); Output: Output: 12 Output: 12.4 Output:   ab Output: , 0 Output: 1.2, 1.3 Output: A, A

8 scanf( ) – Formatted Input
2/22/2019 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, %g the same as those in printf function.

9 scanf( ) – Formatted Input
2/22/2019 scanf( ) – Formatted Input e.g. scanf("%d%*d%d", &a, &b); input: 1234567 result: 12a, 67b e.g. scanf("%3d%*2d%f", &i, &f); input:  result: 123i, 6.789f Form: scanf ("control string", arg1, …, argn); e.g. scanf("%d,%c", &num, &ch); Accessorial format specifications e.g. scanf("%3d%f", &i, &f); input:  result: 123i, f 1 h Precede d, i, o, u, x to read short type integer. 2 l Precede d, i, o, x, u to read long type integer. Precede f, e, g to read double type real number. 3 w Specify the field width of the data to be read. 4 * Specify the input field to be skipped.

10 scanf( ) – Formatted Input
2/22/2019 scanf( ) – Formatted Input e.g. scanf("%d%d%f", &n1, &n2, &f); input: 12345   result: 12n1, 345n2, 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.

11 scanf( ) – Formatted Input
2/22/2019 scanf( ) – Formatted Input e.g. scanf("%d, %d", &a, &b); input: 12, 345  result: 12a, 345b input: 12345  result: 12a, nothingb e.g. scanf("a=%d, b=%d", &a, &b); input: a=12, b=345  result: 12a, 345b input: 12, 345  result: nothinga, nothingb 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.

12 scanf( ) – Formatted Input
2/22/2019 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: 12a result: 12i, '' ch

13 scanf( ) – Formatted Input
2/22/2019 scanf( ) – Formatted Input e.g. scanf("%d%d%f", &n1, &n2, &f); input: 12345   result: 12n1, 345n2, 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.

14 scanf( ) – Formatted Input
2/22/2019 scanf( ) – Formatted Input e.g. scanf("%3d%f", &i, &f); input:  result: 123i, f e.g. scanf("%d%c%f", &a, &b, &c); input: 123a456o.78 result: 123a, 'a'b, 456.0c 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.

15 Program 1 Read in 2 numbers, exchange them, then output them.
2/22/2019 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.

16 Program 1 Please input x and y: 4 5.6 
2/22/2019 Program 1 Please input x and y: 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); }

17 2/22/2019 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.

18 Program 2 Please input a lowercase: m 
2/22/2019 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); }

19 2/22/2019 Homework Review Questions P , 4.2, 4.4, 4.5 (Write down in your exercise book) Programming Exercises


Download ppt "Chapter 4 Managing Input and Output Operations"

Similar presentations


Ads by Google