4 Managing Input and Output Operations

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

Chapter 4

Managing Input and


Output Operations
PROGRAMMING IN ANSI C

10/03/15

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.

scanf( )

getchar( )

gets( )

printf( ) putchar( )

puts( )

e.g. printf( )
# include <stdio.h>
# include "stdio.h"

standard input and output


2

10/03/15

getchar( ) & putchar( )

Form:

abc

variable#include
= getchar();
<stdio.h>
main()
putchar(character);
{
char c ;

abcD10

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

printf("%d", getchar()); }
3

10/03/15

#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();
}

10/03/15

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


appear.

e.g. price amount = $ , .

Escape sequence characters.

Format specifications.

e.g. \n

e.g. %f, %d
5

10/03/15

printf( ) Formatted Output

Form:

printf("control string", arg1, , argn);

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


2.

printf("%c,
%d", 97,list:
'a');(arg1, , argn)
The outputted
expression

Output:
97expression or more than one
There
can be noa,
any
expression.
They
are32767+1,
separated
by comma.
printf("%d,
%u",
32767+1);

Output:
32768
In spite of what-32768,
type these
expressions are, they
are always outputted in specified format.
6

10/03/15

printf( ) Formatted Output

printf ("%%");
("%d, %c",65,
("%x,
("%o,
("%u,
("%c,
("%s","Hello!");
("%f",123.45);
("%e",12.3);
%X",-3,
%O",-3,
%i",-3, 'A');
%U",-3,
'A');
'A');
Output:
177775,
Format specifications
("%g",123.450);
Output:
123.45
%
-3,A65
fffd,
65533,
A,
Hello!
41 %U
%O
123.450000
1.230000e+01
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
Real number either f-type or e-type depending on
9 %g, %G
the length of the value without insignificant zero
10 %%
%
7

10/03/15

printf( ) Formatted Output

Accessorial format specifications

.p

Specifies the minimum field width for the output.


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.
Precede d, i, o, x, u to outputs long type integer.
Precede f, e, g to outputs double type real number.
Left-justified, and remaining field will be blank.

printf ("%2c,
("%3d",%d",
("%3f",
("%.1f",
("%.1f,
("%4.2s",
("%ld,
%-2c",
%.1f",
12.3);
65536,
1.25,
'A', 'A');
Output:
65536,
12);
12.36);
1.251);
"abc");
65536);
Output: 1.2,
12.4
12
A, 1.3
Aab
12.300000
0

10/03/15

scanf( ) Formatted Input

Form:
e.g.
1.

scanf ("control string", arg1, , argn);


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

format specifications
%d, %i, %o, %x, %u, %c, %s, %f, %e, %g
the same as those in printf function.

10/03/15

scanf( ) Formatted Input

e.g.
Form:

scanf("%d%*d%d", &a, &b);


input:
1234567
scanf ("control
string", arg1, , argn);
result:
12a, 67b
e.g.
&num, &ch);
scanf("%3d%f",
&i, &f);
e.g. scanf("%d,%c",
scanf("%3d%*2d%f",
&i, &f);
input:
12345.6789
input:
123456.789
Accessorial format specifications
result:
123i,
result:
123i, 45.6789f
6.789f

2.

Precede d, i, o, u, x to read short type integer.


Precede d, i, o, x, u to read long type integer.

Specify the field width of the data to be read.

Specify the input field to be skipped.

Precede f, e, g to read double type real number.

10

10/03/15

scanf( ) Formatted Input

e.g.
Form:

3.

scanf("%d%d%f",
&f);
scanf
("control string",&n1,
arg1,&n2,
, argn);
input:
12345 
67.89&num, &ch);
e.g. scanf("%d,%c",
result:
12n1, 345n2, 67.89 f
Separators

When it read in the integers or real numbers,


it defaults blank spaces, tabs or newlines as
separators.

11

10/03/15

scanf( ) Formatted Input

e.g.
Form:

scanf("%d,
scanf("a=%d,
b=%d",
&a,arg1,
&b);
&a,
scanf
("control %d",
string",
, &b);
argn);
input:
12, 345
a=12,
b=345


12a, &num,
345b
e.g.result:
scanf("%d,%c",
&ch);

3.

input:
12,
12345
345 
Separators
result:
12a,
nothinga,
nothingb
nothingb
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

10/03/15

scanf( ) Formatted Input

Form:
scanf ("control string", arg1, , argn);
e.g.
scanf("%d%c", &i, &ch);
12a &num, &ch);
e.g.input:
scanf("%d,%c",
result:
12i, '' 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.

13

10/03/15

scanf( ) Formatted Input

e.g.
Form:

4.

scanf("%d%d%f",
&f);
scanf
("control string",&n1,
arg1,&n2,
, argn);
input:
12345 
67.89&num, &ch);
e.g. scanf("%d,%c",
result:
12n1, 345n2, 67.89 f
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

10/03/15

scanf( ) Formatted Input

e.g.
Form:
e.g.
4.

scanf("%3d%f",
scanf("%d%c%f",
&i,&a,
&f);
&c);
scanf ("control
string", arg1,
,&b,
argn);
input:
12345.6789
123a456o.78
scanf("%d,%c",
&num,
&ch); 456.0c
result:
123i,
123a,
45.6789f
'a'b,

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

10/03/15

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:

Step4:

x -> temp

(temp = x;)

y -> x

(x = y;)

temp -> y

(y = temp;)

Output the value of x and y.


16

10/03/15

Program 1
main()
{

Please input x and y:


4 5.6
Before exchange: x=4.00, y=5.60
float x, y, temp;
After exchange: x=5.60, y=4.00

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

10/03/15

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

10/03/15

Program
Please
input 2
a lowercase:
m
The lowercase is m, and its ASCII value is 109
#include <stdio.h>
The uppercase equivalent is M, and its ASCII value is
main()
77
{
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

10/03/15

#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();
}
20

10/03/15

Homework

Review Questions P106


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

Programming Exercises

21

You might also like