4 Managing Input and Output Operations

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

description

kkh

Transcript of 4 Managing Input and Output Operations

Page 1: 4 Managing Input and Output Operations

Chapter 4Managing Input and Output Operations

PROGRAMMING IN ANSI C

Page 2: 4 Managing Input and Output Operations

04/18/23

2

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 st

andard input/output functions. e.g. printf( )

scanf( ) getchar( ) gets( )

printf( ) putchar( ) puts( )# include <stdio.h>

# include "stdio.h"

standard input and output

Page 3: 4 Managing Input and Output Operations

04/18/23

3

getchar( ) & putchar( )

Form:variable = getchar();

putchar(character);

#include <stdio.h> main() { char c ;

c = getchar ( );putchar ( c );putchar ( getchar () );printf ( "%c", getchar () );putchar ('D');

}

abcabcD

#include <stdio.h> main() { char c ;

c = getchar ( );putchar ( c );putchar ( getchar () );printf ( "%c", getchar () );putchar ('D'); printf("%d", getchar()); }

10

Page 4: 4 Managing Input and Output Operations

04/18/23

4

#include "stdio.h" #include "conio.h" #include "ctype.h" main() { char character; printf("press any key\n"); character=getchar(); if(isalpha(character)>0) printf("The character is a letter."); else if(isdigit(character)>0) printf("The character is a digit"); else printf("The character is not alphanumeric."); getch(); }

Page 5: 4 Managing Input and Output Operations

04/18/23

5

printf( ) – Formatted Output

Form: printf("control string", arg1, …, argn);

e.g. printf("price=$%.2f\n, amount=%d.", pr*am, am);

1. Control string

The characters that will be outputted as they appe

ar. e.g. price amount = $ , .

Escape sequence characters. e.g. \n

Format specifications. e.g. %f, %d

Page 6: 4 Managing Input and Output Operations

04/18/23

6

printf( ) – Formatted Output

Form: printf("control string", arg1, …, argn);

e.g. printf("price=$%.2f\n, amount=%d.", pr*am, am);

2. 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 a

re always outputted in specified format.

printf("%c, %d", 97, 'a');

Output: a, 97printf("%d, %u", 32767+1, 32767+1);

Output: -32768, 32768

Page 7: 4 Managing Input and Output Operations

04/18/23

7

Output: -3, 65Output: fffd, 41printf ("%d, %i",-3, 'A');printf ("%x, %X",-3, 'A');

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 integer5 %c Single character6 %s Sting7 %f Real number in decimal notation8 %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 %% %

printf( ) – Formatted Output Format 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 ("%%");

Page 8: 4 Managing Input and Output Operations

04/18/23

8

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: abprintf ("%4.2s", "abc");Output: 65536, 0

printf ("%ld, %d", 65536, 65536);Output: A, Aprintf ("%2c, %-2c", 'A', 'A');

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( ) – Formatted Output Accessorial format specifications

Page 9: 4 Managing Input and Output Operations

04/18/23

9

scanf( ) – Formatted Input

Form: scanf ("control string", arg1, …, argn);

e.g. scanf("%d,%c", &num, &ch);

1. format specifications

%d, %i, %o, %x, %u, %c, %s, %f, %e, %g

the same as those in printf function.

Page 10: 4 Managing Input and Output Operations

04/18/23

10

scanf( ) – Formatted Input

Form:scanf ("control string", arg1, …, argn);

e.g. scanf("%d,%c", &num, &ch);

2. Accessorial format specifications

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.

e.g. scanf("%3d%f", &i, &f); input: 12345.6789 result:123i, 45.6789f

e.g. scanf("%d%*d%d", &a, &b);� � input: 1234567 result: 12a, 67be.g. scanf("%3d%*2d%f", &i, &f); input: 123456.789 result:123i, 6.789f

Page 11: 4 Managing Input and Output Operations

04/18/23

11

scanf( ) – Formatted Input

Form:scanf ("control string", arg1, …, argn);

e.g. scanf("%d,%c", &num, &ch);

3. Separators

① When it read in the integers or real

numbers, it defaults blank spaces, tabs or

newlines as separators.

e.g. scanf("%d%d%f", &n1, &n2, &f); input: 12345 �

67.89 result: 12n1, 345n2, 67.89 f

Page 12: 4 Managing Input and Output Operations

04/18/23

12

scanf( ) – Formatted Input

Form:scanf ("control string", arg1, …, argn);

e.g. scanf("%d,%c", &num, &ch);

3. 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.

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

Page 13: 4 Managing Input and Output Operations

04/18/23

13

scanf( ) – Formatted Input

Form:scanf ("control string", arg1, …, argn);

e.g. scanf("%d,%c", &num, &ch);

3. 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

Page 14: 4 Managing Input and Output Operations

04/18/23

14

scanf( ) – Formatted Input

Form:scanf ("control string", arg1, …, argn);

e.g. scanf("%d,%c", &num, &ch);

4. 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", &n1, &n2, &f); input: 12345 �

67.89 result: 12n1, 345n2, 67.89 f

Page 15: 4 Managing Input and Output Operations

04/18/23

15

scanf( ) – Formatted Input

Form: scanf ("control string", arg1, …, argn);

e.g. scanf("%d,%c", &num, &ch);

4. How to judge a data is finished?

② When the input data achieves the specified fiel

d wide, “scanf” considers the data is finished.

③ When “scanf” encounters an illegal characte

r, it considers the data is finished.

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

Page 16: 4 Managing Input and Output Operations

04/18/23

16

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.

Page 17: 4 Managing Input and Output Operations

04/18/23

17

Program 1

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);

}

Please input x and y:4 5.6 Before exchange: x=4.00, y=5.60After exchange: x=5.60, y=4.00

Page 18: 4 Managing Input and Output Operations

04/18/23

18

Program 2

Read in a lowercase letter, convert it into its uppercas

e equivalent, and then output the uppercase and its A

SCII 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.

Page 19: 4 Managing Input and Output Operations

04/18/23

19

Program 2

#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 A

SCII value is %d\n", ch, ch);}

Please input a lowercase:The lowercase is m, and its ASCII value is 109The uppercase equivalent is M, and its ASCII value is 77

m

Page 20: 4 Managing Input and Output Operations

04/18/23

20

#include "stdio.h" #include "conio.h" #include "ctype.h" main() { char x; x=getchar(); if(islower(x)>0) x-=32; else if(isupper(x)) x+=32; printf("x=%c\n",x); getch(); }

Page 21: 4 Managing Input and Output Operations

04/18/23

21

Homework

Review Questions P106

4.1, 4.2, 4.4, 4.5 (Write down in your exercise

book)

Programming Exercises