4 Managing Input and Output Operations
4 Managing Input and Output Operations
4 Managing Input and Output Operations
10/03/15
Chapter 4
scanf( )
getchar( )
gets( )
printf( ) putchar( )
puts( )
e.g. printf( )
# include <stdio.h>
# include "stdio.h"
10/03/15
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
Form:
Control string
Format specifications.
e.g. \n
e.g. %f, %d
5
10/03/15
Form:
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 ("%%");
("%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
.p
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
Form:
e.g.
1.
format specifications
%d, %i, %o, %x, %u, %c, %s, %f, %e, %g
the same as those in printf function.
10/03/15
e.g.
Form:
2.
10
10/03/15
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
11
10/03/15
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
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
13
10/03/15
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?
10/03/15
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,
10/03/15
Program 1
Step2:
Step3:
Exchange them:
Step4:
x -> temp
(temp = x;)
y -> x
(x = y;)
temp -> y
(y = temp;)
10/03/15
Program 1
main()
{
10/03/15
Program 2
Step2:
Step3:
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
Programming Exercises
21