Chapter 9 Lab 1 - Calculus 1 | Trường Đại học Quốc tế, Đại học Quốc gia Thành phố HCM

Chapter 9 Lab 1 - Calculus 1 | Trường Đại học Quốc tế, Đại học Quốc gia Thành phố HCM được sưu tầm và soạn thảo dưới dạng file PDF để gửi tới các bạn sinh viên cùng tham khảo, ôn tập đầy đủ kiến thức, chuẩn bị cho các buổi học thật tốt. Mời bạn đọc đón xem!

Môn:

Calculus 1 (MA001IU) 42 tài liệu

Thông tin:
23 trang 7 tháng trước

Bình luận

Vui lòng đăng nhập hoặc đăng ký để gửi bình luận.

Chapter 9 Lab 1 - Calculus 1 | Trường Đại học Quốc tế, Đại học Quốc gia Thành phố HCM

Chapter 9 Lab 1 - Calculus 1 | Trường Đại học Quốc tế, Đại học Quốc gia Thành phố HCM được sưu tầm và soạn thảo dưới dạng file PDF để gửi tới các bạn sinh viên cùng tham khảo, ôn tập đầy đủ kiến thức, chuẩn bị cho các buổi học thật tốt. Mời bạn đọc đón xem!

37 19 lượt tải Tải xuống
1
1
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Chapter 9 - Formatted Input/Output
Outline
9.1 Introduction
9.2 Streams
9.3 Formatting Output with
p
p
p
pp
r
r
r
rr
i
i
i
ii
n
n
n
nn
t
t
t
tt
f
f
f
ff
9.4 Printing Integers
9.5 Printing Floating-Point Numbers
9.6 Printing Strings and Characters
9.7 Other Conversion Specifiers
9.8 Printing with Field Widths and Precisions
9.9 Using Flags in the
p
p
p
pp
r
r
r
rr
i
i
i
ii
n
n
n
nn
t
t
t
tt
f
f
f
ff Format-Control String
9.10 Printing Literals and Escape Sequences
9.11 Formatting Input with
s
s
s
ss
c
c
c
cc
a
a
a
aa
n
n
n
nn
f
f
f
ff
2
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Objectives
In this chapter, you will learn:
To understand input and output streams.
To be able to use all print formatting capabilities.
To be able to use all input formatting capabilities.
2
3
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9.1 Introduction
In this chapter
Presentation of results
scanf and pr i nt f
Streams (input and output)
get s, , , (in put s get char put char <st di o. h>)
4
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9.2 Streams
Streams
Sequences of characters organized into lines
Each line consists of zero or more characters and ends with
newline character
ANSI C must support lines of at least 254 characters
Performs all input and output
Can often be redirected
Standard input – keyboard
Standard output – screen
Standard error – screen
More in Chapter 11
3
5
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9.3 Formatting Output with
p
p
p
pp
r
r
r
rr
i
i
i
ii
n
n
n
nn
t
t
t
tt
f
f
f
ff
pr i nt f
Precise output formatting
Conversion specifications: flags, field widths, precisions, etc.
Can perform rounding, aligning columns, right/left
justification, inserting literal characters, exponential format,
hexadecimal format, and fixed width and precision
Format
pr i nt f ( format-control-string, other-arguments );
Format control string: describes output format
Other-arguments: correspond to each conversion
specification in format-control-string
Each specification begins with a percent sign(%), ends with
conversion specifier
6
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9.4 Printing Integers
Conversion Specifier Description
d
Display a signed decimal integer.
i
Display a signed decimal integer. (Note: The i and d specifiers
are different when used with scanf .)
o
Display an unsigned octal integer.
u
Display an unsigned decimal integer.
x or X
Display an unsigned hexadecimal integer. X causes the digits 0- 9
and the letters A- F to be displayed and x causes the digits 0- 9
and a- f to be displayed.
h
or l l (letter )
Place before any integer conversion specifier to indicate that a
shor t or l ong integer is displayed respectively. Letters h and l
are more precisely called length modifiers.
Fig. 9.1 Integer conversion specifiers.
4
7
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9.4 Printing Integers
Integer
Whole number (no decimal point): , , 25 0 - 9
Positive, negative, or zero
Only minus sign prints by default (later we shall change this)
Outline
8
Outline
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson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
a
a
aa
t
t
t
tt
i
i
i
ii
o
o
o
oo
n
n
n
nn
*
*
*
**
/
/
/
//
20
21
}
}
}
}}
/
/
/
//
*
*
*
**
e
e
e
ee
n
n
n
nn
d
d
d
d d
m
m
m
mm
a
a
a
aa
i
i
i
ii
n
n
n
nn
*
*
*
**
/
/
/
//
5
Outline
9
Outline
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Program Ouptut
4
4
4
44
55
55
55
5555
4
4
4
44
55
55
55
5555
4
4
4
44
55
55
55
5555
-
-
-
--
45
45
45
4545
5
5
5
55
3
3
3
33
20
20
20
2020
0
0
0
00
0
0
0
00
2
2
2
22
0
0
0
00
0
0
0
00
0
0
0
00
0
0
0
00
0
0
0
00
0
0
0
00
0
0
0
00
0
0
0
00
0
0
0
00
7
7
7
77
07
07
07
0707
4
4
4
44
55
55
55
5555
4
4
4
44
2
2
2
22
9
9
9
99
4
4
4
44
9
9
9
99
6
6
6
66
6
6
6
66
8
8
8
88
4
4
4
44
1
1
1
11
1
1
1
11
c
c
c
cc
7
7
7
77
1
1
1
11
C
C
C
CC
7
7
7
77
10
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9.5 Printing Floating-Point Numbers
Floating Point Numbers
Have a decimal point (33. 5)
Exponential notation (computer's version of scientific
notation)
150. 3 is 1. 503 x 10² in scientific
150. 3 is 1. 503E+02 in exponential (E stands for exponent)
use e or E
f print floating point with at least one digit to left of
decimal
g (or G) - prints in f or e with no trailing zeros (1. 2300
becomes 1. 23)
Use exponential if exponent less than - 4, or greater than or
equal to precision (6 digits by default)
6
11
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9.5 Printing Floating-Point Numbers
Conversion specifier
Description
e or E Display a floating-point value in exponential notation.
f
Display floating-point values.
g or G Display a floating-point value in either the floating-point form f or the
exponential form e (or E).
L
Place before any floating-point conversion specifier to indicate that a
l ong doubl e floating-point value is displayed.
Fig. 9.3 Floating-point conversion specifiers.
Outline
12
Outline
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
fig09_04.c
Program Output
1.234568e+006
1.234568e+006
-1.234568e+006
1.234568E+006
1234567.890000
1.23457e+006
1.23457E+006
1
/
/
/
//
*
*
*
**
Fi
Fi
Fi
FiFi
g
g
g
g g
4
4
4
44
:
:
:
::
*
*
*
**
/
/
/
//
9
9
9
99
.
.
.
..
f
f
f
ff
i
i
i
ii
g
g
g
gg
0
0
0
00
9
9
9
99
_
_
_
__
04
04
04
0404
.
.
.
..
c
c
c
cc
2
/
/
/
//
*
*
*
**
Pr
Pr
Pr
PrPr
n
n
n
nn
u
u
u
uu
i
i
i
ii
n
n
n
nn
t
t
t
tt
i
i
i
ii
ng
ng
ng
ng ng
f
f
f
ff
l
l
l
ll
o
o
o
oo
a
a
a
aa
t
t
t
tt
i
i
i
ii
ng
ng
ng
ngng
-
-
-
--
p
p
p
pp
o
o
o
oo
i
i
i
ii
n
n
n
nn
t
t
t
tt
m
m
m
mm
b
b
b
bb
e
e
e
ee
r
r
r
rr
s
s
s
ss
w
w
w
ww
i
i
i
ii
t
t
t
tt
h
h
h
hh
3
f
f
f
ff
l
l
l
ll
o
o
o
oo
a
a
a
aa
t
t
t
tt
i
i
i
ii
n
n
n
nn
g
g
g
gg
-
-
-
--
p
p
p
pp
o
o
o
oo
i
i
i
ii
n
n
n
nn
t
t
t
tt
c
c
c
cc
o
o
o
oo
n
n
n
nn
v
v
v
vv
e
e
e
ee
r
r
r
rr
s
s
s
ss
i
i
i
ii
o
o
o
oo
n
n
n
nn
s
s
s
ss
p
p
p
pp
e
e
e
ee
c
c
c
cc
i
i
i
ii
f
f
f
ff
i
i
i
ii
er
er
er
erer
s
s
s
ss
*
*
*
**
/
/
/
//
4
5
#i
#i
#i
#i#i
n
n
n
nn
c
c
c
cc
l
l
l
ll
ud
ud
ud
udud
e
e
e
ee
<
<
<
<<
s
s
s
ss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
18
19
}
}
}
}}
/
/
/
//
*
*
*
**
e
e
e
ee
n
n
n
nn
d
d
d
dd
m
m
m
mm
a
a
a
aa
i
i
i
ii
n
n
n
nn
*
*
*
**
/
/
/
//
7
13
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9.6 Printing Strings and Characters
c
Prints char argument
Cannot be used to print the first character of a string
s
Requires a pointer to c har as an argument
Prints characters until NULL (' \ 0' ) encountered
Cannot print a char argument
Remember
Single quotes for character constants (' z' )
Double quotes for strings " z" (which actually contains two
characters, ' z' and ' \ 0' )
Outline
14
Outline
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
fig09_05.c
A
A
A
AA
Th
Th
Th
ThTh
i
i
i
ii
s
s
s
ss
i
i
i
ii
s
s
s
ss
a
a
a
aa
s
s
s
ss
t
t
t
tt
r
r
r
rr
i
i
i
ii
ng
ng
ng
ngng
Th
Th
Th
ThTh
i
i
i
ii
s
s
s
ss
i
i
i
ii
s
s
s
ss
a
a
a
aa
s
s
s
ss
t
t
t
tt
r
r
r
rr
i
i
i
ii
ng
ng
ng
ngng
Th
Th
Th
ThTh
i
i
i
ii
s
s
s
ss
i
i
i
ii
s
s
s
ss
a
a
a
aa
l
l
l
ll
s
s
s
ss
o
o
o
oo
a
a
a
aa
s
s
s
ss
t
t
t
tt
r
r
r
rr
i
i
i
ii
ng
ng
ng
ngng
1
/
/
/
//
*
*
*
**
Fi
Fi
Fi
FiFi
g
g
g
gg
9
9
9
99
.
.
.
..
5
5
5
55
:
:
:
::
f
f
f
ff
i
i
i
ii
g
g
g
gg
0
0
0
00
9
9
9
99
_
_
_
__
05
05
05
0505
c
c
c
cc
*
*
*
**
/
/
/
//
2
/
/
/
//
*
*
*
**
Pr
Pr
Pr
PrPr
i
i
i
ii
n
n
n
nn
t
t
t
tt
i
i
i
ii
n
n
n
nn
g
g
g
gg
s
s
s
ss
t
t
t
tt
r
r
r
rr
i
i
i
ii
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p
p
p
pp
r
r
r
rr
i
i
i
ii
n
n
n
nn
t
t
t
tt
f
f
f
ff
(
(
(
((
"
"
"
""
%
%
%
%%
s
s
s
ss
\
\
\
\\
n
n
n
nn
"
"
"
""
,
,
,
,,
s
s
s
ss
t
t
t
tt
r
r
r
rr
i
i
i
ii
ng
ng
ng
ngng
)
)
)
))
;
;
;
;;
14
p
p
p
pp
r
r
r
rr
i
i
i
ii
n
n
n
nn
t
t
t
tt
f
f
f
ff
(
(
(
((
"
"
"
""
%
%
%
%%
s
s
s
ss
\
\
\
\\
n
n
n
nn
"
"
"
""
,
,
,
,,
s
s
s
ss
t
t
t
tt
r
r
r
rr
i
i
i
ii
ng
ng
ng
ngng
P
P
P
PP
t
t
t
tt
r
r
r
rr
)
)
)
))
;
;
;
;;
15
16
;
;
;
;;
r
r
r
rr
e
e
e
ee
t
t
t
tt
u
u
u
uu
r
r
r
rr
n
n
n
nn
0
0
0
00
/
/
/
//
*
*
*
**
i
i
i
ii
n
n
n
nn
d
d
d
dd
i
i
i
ii
c
c
c
cc
a
a
a
aa
t
t
t
tt
e
e
e
ee
s
s
s
ss
s
s
s
ss
u
u
u
uu
c
c
c
cc
c
c
c
cc
es
es
es
eses
s
s
s
ss
f
f
f
ff
u
u
u
uu
l
l
l
ll
t
t
t
tt
e
e
e
ee
r
r
r
rr
m
m
m
mm
i
i
i
ii
na
na
na
nana
t
t
t
tt
i
i
i
ii
on
on
on
onon
*
*
*
**
/
/
/
//
17
18
}
}
}
}}
/
/
/
//
*
*
*
**
e
e
e
ee
n
n
n
nn
d
d
d
d d
m
m
m
mm
a
a
a
aa
i
i
i
ii
n
n
n
nn
*
*
*
**
/
/
/
//
8
15
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9.7 Other Conversion Specifiers
p
Displays pointer value (address)
n
Stores number of characters already output by current
pr i nt f statement
Takes a pointer to an integer as an argument
Nothing printed by a %n specification
Every pr i nt f call returns a value
Number of characters output
Negative number if error occurs
%
Prints a percent sign
%%
16
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9.7 Other Conversion Specifiers
Conversion specifier
Description
p
Display a pointer value in an implementation-defined manner.
n
Store the number of characters already output in the current
pr i nt f statement. A pointer to an integer is supplied as the
corresponding argument. Nothing is displayed.
%
Display the percent character.
Fig. 9.6 Other conversion specifiers.
9
Outline
17
Outline
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
fig09_07.c (1 of 2)
1
/
/
/
//
*
*
*
**
Fi
Fi
Fi
FiFi
g
g
g
g g
9
9
9
99
.
.
.
..
7
7
7
77
:
:
:
::
f
f
f
ff
i
i
i
ii
g0
g0
g0
g0g0
9_
9_
9_
9_9_
0
0
0
00
7
7
7
77
.
.
.
..
c
c
c
cc
*
*
*
**
/
/
/
//
2
/
/
/
//
*
*
*
**
U
U
U
UU
s
s
s
ss
i
i
i
ii
ng
ng
ng
ngng
t
t
t
tt
h
h
h
hh
e
e
e
e e
p
p
p
pp
,
,
,
,,
n
n
n
nn
,
,
,
,,
a
a
a
aa
n
n
n
nn
d
d
d
dd
%
%
%
%%
c
c
c
cc
o
o
o
oo
nv
nv
nv
nvnv
e
e
e
ee
r
r
r
rr
s
s
s
ss
i
i
i
ii
o
o
o
oo
n
n
n
nn
s
s
s
ss
p
p
p
pp
e
e
e
ee
c
c
c
cc
i
i
i
ii
f
f
f
ff
i
i
i
ii
er
er
er
erer
s
s
s
ss
*
*
*
**
/
/
/
//
3
#i
#i
#i
#i#i
n
n
n
nn
c
c
c
cc
l
l
l
ll
ud
ud
ud
udud
e
e
e
ee
<s
<s
<s
<s<s
t
t
t
tt
d
d
d
dd
i
i
i
ii
o.
o.
o.
o.o.
h
h
h
hh
>
>
>
> >
4
5
i
i
i
ii
n
n
n
nn
t
t
t
tt
m
m
m
mm
ai
ai
ai
aiai
n
n
n
nn
(
(
(
((
)
)
)
))
6
{
{
{
{{
7
*
*
*
**
pt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
ng
ng
ngng
\
\
\
\\
n
n
n
nn
"
"
"
""
)
)
)
))
;
;
;
;;
22
23
;
;
;
;;
r
r
r
rr
e
e
e
ee
t
t
t
tt
u
u
u
uu
r
r
r
rr
n
n
n
nn
0
0
0
00
/
/
/
//
*
*
*
**
i
i
i
ii
n
n
n
nn
d
d
d
dd
i
i
i
ii
c
c
c
cc
a
a
a
aa
t
t
t
tt
e
e
e
ee
s
s
s
ss
s
s
s
ss
u
u
u
uu
c
c
c
cc
c
c
c
cc
es
es
es
eses
s
s
s
ss
f
f
f
ff
u
u
u
uu
l
l
l
ll
t
t
t
tt
e
e
e
ee
r
r
r
rr
m
m
m
mm
i
i
i
ii
na
na
na
nana
t
t
t
tt
i
i
i
ii
on
on
on
onon
*
*
*
**
/
/
/
//
24
25
}
}
}
}}
/
/
/
//
*
*
*
**
e
e
e
ee
n
n
n
nn
d
d
d
dd
m
m
m
mm
ai
ai
ai
aiai
n
n
n
nn
*
*
*
**
/
/
/
//
Outline
18
Outline
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
The value of ptr is 0012FF78
The address of x is 0012FF78
Total characters printed on this line: 38
This line has 28 characters
28 characters were printed
Printing a % in a format control string
Program Output
10
19
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9.8 Printing with Field Widths and
Precisions
Field width
Size of field in which data is printed
If width larger than data, default right justified
If field width too small, increases to fit data
Minus sign uses one character position in field
Integer width inserted between %and conversion specifier
%4d field width of 4
20
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9.8 Printing with Field Widths and
Precisions
Precision
Meaning varies depending on data type
Integers (default 1)
Minimum number of digits to print
If data too small, prefixed with zeros
Floating point
Number of digits to appear after decimal (e and f )
For g maximum number of significant digits
Strings
Maximum number of characters to be written from string
Format
Use a dot (. ) then precision number after %
%. 3f
11
21
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9.8 Printing with Field Widths and
Precisions
Field width and precision
Can both be specified
%wi dt h. pr eci si on
%5. 3f
Negative field width – left justified
Positive field width – right justified
Precision must be positive
Can use integer expressions to determine field width and
precision values
Place an asterisk (* ) in place of the field width or precision
Matched to an i nt argument in argument list
Example:
pr i nt f ( " %* . * f " , 7, 2, 98. 736 ) ;
Outline
22
Outline
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
fig09_08.c
1
/
/
/
//
*
*
*
**
Fi
Fi
Fi
FiFi
g
g
g
g g
9
9
9
99
.
.
.
..
8
8
8
88
:
:
:
::
f
f
f
ff
i
i
i
ii
g0
g0
g0
g0g0
9
9
9
99
_
_
_
__
0
0
0
00
8
8
8
88
.
.
.
..
c
c
c
cc
*
*
*
**
/
/
/
//
2
/
/
/
//
*
*
*
**
Pr
Pr
Pr
PrPr
i
i
i
ii
n
n
n
nn
t
t
t
tt
i
i
i
ii
n
n
n
nn
g
g
g
g g
i
i
i
ii
n
n
n
nn
t
t
t
tt
e
e
e
ee
g
g
g
gg
e
e
e
ee
r
r
r
rr
s
s
s
ss
r
r
r
rr
i
i
i
ii
g
g
g
gg
h
h
h
hh
t
t
t
tt
-
-
-
--
j
j
j
jj
us
us
us
usus
t
t
t
tt
i
i
i
ii
f
f
f
ff
i
i
i
ii
ed
ed
ed
eded
*
*
*
**
/
/
/
//
3
#
#
#
##
i
i
i
ii
nc
nc
nc
ncnc
l
l
l
ll
u
u
u
uu
d
d
d
dd
e
e
e
ee
<s
<s
<s
<s<s
t
t
t
tt
d
d
d
dd
i
i
i
ii
o.
o.
o.
o.o.
h
h
h
hh
>
>
>
>>
4
5
i
i
i
ii
nt
nt
nt
ntnt
m
m
m
mm
a
a
a
aa
i
i
i
ii
n(
n(
n(
n(n(
)
)
)
))
6
{
{
{
{{
7
p
p
p
pp
r
r
r
rr
i
i
i
ii
nt
nt
nt
ntnt
f
f
f
ff
(
(
(
((
"
"
"
""
%
%
%
%%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e
e
e
ee
n
n
n
nn
d
d
d
dd
m
m
m
mm
a
a
a
aa
i
i
i
ii
n
n
n
nn
*
*
*
**
/
/
/
//
12
Outline
23
Outline
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1
1
1
11
1
1
1
11
2
2
2
22
1
1
1
11
2
2
2
22
3
3
3
33
1
1
1
11
23
23
23
2323
4
4
4
44
1
1
1
11
23
23
23
2323
4
4
4
44
5
5
5
55
-
-
-
--
1
1
1
11
-
-
-
--
1
1
1
11
2
2
2
22
-
-
-
--
12
12
12
1212
3
3
3
33
-
-
-
--
12
12
12
1212
3
3
3
33
4
4
4
44
-
-
-
--
12
12
12
1212
3
3
3
33
45
45
45
4545
Program Output
Outline
24
Outline
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
fig09_09.c
1
/
/
/
//
*
*
*
**
Fi
Fi
Fi
FiFi
g
g
g
g g
9
9
9
99
.
.
.
..
9
9
9
99
:
:
:
::
f
f
f
ff
i
i
i
ii
g
g
g
gg
0
0
0
00
9
9
9
99
_
_
_
__
0
0
0
00
9
9
9
99
.
.
.
..
c
c
c
cc
*
*
*
**
/
/
/
//
2
/
/
/
//
*
*
*
**
U
U
U
UU
s
s
s
ss
i
i
i
ii
ng
ng
ng
ngng
p
p
p
pp
r
r
r
rr
e
e
e
ee
c
c
c
cc
i
i
i
ii
s
s
s
ss
i
i
i
ii
on
on
on
onon
w
w
w
ww
h
h
h
hh
i
i
i
ii
l
l
l
ll
e
e
e
ee
p
p
p
pp
r
r
r
rr
i
i
i
ii
n
n
n
nn
t
t
t
tt
i
i
i
ii
n
n
n
nn
g
g
g
gg
i
i
i
ii
nt
nt
nt
ntnt
e
e
e
ee
g
g
g
gg
e
e
e
ee
r
r
r
rr
s
s
s
ss
,
,
,
,,
3
f
f
f
ff
l
l
l
ll
o
o
o
oo
at
at
at
atat
i
i
i
ii
n
n
n
nn
g-
g-
g-
g-g-
po
po
po
popo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;
19
pr
pr
pr
prpr
i
i
i
ii
n
n
n
nn
t
t
t
tt
f
f
f
ff
(
(
(
((
"
"
"
""
\
\
\
\\
t
t
t
tt
%
%
%
%%
.
.
.
..
11
11
11
1111
s
s
s
ss
\
\
\
\\
n"
n"
n"
n"n"
,
,
,
,,
s
s
s
ss
)
)
)
))
;
;
;
;;
20
21
r
r
r
rr
et
et
et
etet
u
u
u
uu
r
r
r
rr
n
n
n
nn
0
0
0
00
;
;
;
;;
/
/
/
//
*
*
*
**
i
i
i
ii
n
n
n
nn
d
d
d
dd
i
i
i
ii
c
c
c
cc
a
a
a
aa
t
t
t
tt
e
e
e
ee
s
s
s
ss
s
s
s
ss
uc
uc
uc
ucuc
c
c
c
cc
e
e
e
ee
s
s
s
ss
s
s
s
ss
f
f
f
ff
u
u
u
uu
l
l
l
ll
t
t
t
tt
er
er
er
erer
m
m
m
mm
i
i
i
ii
n
n
n
nn
at
at
at
atat
i
i
i
ii
o
o
o
oo
n
n
n
nn
*
*
*
**
/
/
/
//
22
23
}
}
}
}}
/
/
/
//
*
*
*
**
en
en
en
enen
d
d
d
dd
m
m
m
mm
a
a
a
aa
i
i
i
ii
n
n
n
n n
*
*
*
**
/
/
/
//
| 1/23

Preview text:

1
Chapter 9 - Formatted Input/Output Outline 9.1 Introduction 9.2 Streams 9.3
Formatting Output with pr r i i nt t f 9.4 Printing Integers 9.5
Printing Floating-Point Numbers 9.6
Printing Strings and Characters 9.7
Other Conversion Specifiers 9.8
Printing with Field Widths and Precisions 9.9 Using Flags in the p r i n t f Format-Control String 9.10
Printing Literals and Escape Sequences 9.11
Formatting Input with s c anf
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives
• In this chapter, you will learn:
– To understand input and output streams.
– To be able to use all print formatting capabilities.
– To be able to use all input formatting capabilities.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 3 9.1 Introduction • In this chapter – Presentation of results – scanf and pr i nt f – Streams (input and output)
• get s , put s , get char , put c har (in )
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 9.2 Streams • Streams
– Sequences of characters organized into lines
• Each line consists of zero or more characters and ends with newline character
• ANSI C must support lines of at least 254 characters
– Performs all input and output – Can often be redirected
• Standard input – keyboard • Standard output – screen • Standard error – screen • More in Chapter 11
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 5 9.3
Formatting Output with p r i n t f • pr i nt f – Precise output formatting
• Conversion specifications: flags, field widths, precisions, etc.
– Can perform rounding, aligning columns, right/left
justification, inserting literal characters, exponential format,
hexadecimal format, and fixed width and precision • Format
– pr i nt f ( format-control-string, other-arguments );
– Format control string: describes output format
– Other-arguments: correspond to each conversion
specification in format-control-string
• Each specification begins with a percent sign(%), ends with conversion specifier
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 9.4 Printing Integers Conversion Specifier Description d
Display a signed decimal integer. i
Display a signed decimal integer. (Note: The i and d specifiers
are different when used with scanf .) o
Display an unsigned octal integer. u
Display an unsigned decimal integer. x or X
Display an unsigned hexadecimal integer. X causes the digits 0- 9
and the letters A- F to be displayed and x causes the digits 0- 9 and a- f to be displayed. h or l (letter l )
Place before any integer conversion specifier to indicate that a
shor t or l ong integer is displayed respectively. Letters h and l
are more precisely called length modifiers.
Fig. 9.1 Integer conversion specifiers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 7 9.4 Printing Integers • Integer
– Whole number (no decimal point): 25, 0, - 9
– Positive, negative, or zero
– Only minus sign prints by default (later we shall change this)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 / * F i F g 9 . 2 : f ig09_02 . c * / 8 2 / * Us i ng g t he h i n t n ege r con o ve r s i on o spec i f i e r e s * / Ou O t u l t iln i e n 3 # i nc l ude < s t d io . h> > 4 fig09_02.c 5 in t ma in ( ) 6 { 7 p r p i n t f ( " %d \ n " , 455 5 ) ; 8 p r p i n t f ( " %i \ n " , 455 ) ; / * i same as d i n p r i n t f * / 9 p r p i n t f ( " %d \ n " , +4 + 55 ) ; 10 p r i n t f ( " %d \ n " , - 455 5 ) ; 11 p r i n t f ( " %hd \ d n " , 32000 0 ) ; 12 p r i n t f ( " %l d \ d n " , 20000 0 00000 ) ; 13 p r i n t f ( " %o \ n " , 455 5 ) ; 14 p r i n t f ( " %u \ n " , 455 ) ; 15 p r i n t f ( " %u \ n " , - 45 4 5 ) ; 16 p r i n t f ( " % x \ n " , 455 5 ) ; 17 p r i n t f ( " %X \ X n " , 455 5 ) ; 18 19 r e t e u r n 0 ; / * i nd i d ca t es su c ce s e s f u l t e r e mi na t i on * / 20 21 } / * end d ma i n * /
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 9 455 5 Ou O t u l t iln i e n 455 5 455 5 - 45 4 5 Program Ouptut 320 2 00 2000000000 707 0 455 5 4294966841 1c7 1C7
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 9.5
Printing Floating-Point Numbers • Floating Point Numbers
– Have a decimal point (33. 5)
– Exponential notation (computer's version of scientific notation)
• 150. 3 is 1. 503 x 10² in scientific
• 150. 3 is 1. 503E+02 in exponential (E stands for exponent) • use e or E
– f – print floating point with at least one digit to left of decimal
– g (or G) - prints in f or e with no trailing zeros (1. 2300 becomes 1. 23)
• Use exponential if exponent less than - 4, or greater than or
equal to precision (6 digits by default)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 11 9.5
Printing Floating-Point Numbers Conversion specifier Description e or E
Display a floating-point value in exponential notation. f
Display floating-point values. g or G
Display a floating-point value in either the floating-point form f or the exponential form e (or E). L
Place before any floating-point conversion specifier to indicate that a
l ong doubl e floating-point value is displayed. Fig. 9.3
Floating-point conversion specifiers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 / * Fi F i F g g 9 . 4 : f i g09_04 0 . c * / 12 2 / * Pr P r P i n t i ng f l oa t i ng n - po i n t numbe r s w i t h Ou O t u l t iln i e n 3 f l oa t i ng - po i n t c onv e r s i on s pe c i f i er e r e s * / 4 fig09_04.c 5 #i # i nc l l ud u e t d i o . h > 6 7 i i nt t mai i n( n ( ) ) 8 { 9 p r i n t f ( " %e \ n " , 1234 3 567. 7 . 89 ) ; 10 p r i n t f ( " %e \ n " , +12 1 34567 . 89 ) ; 11 p r i n t f ( " %e \ n " , - 1234567 6 . 89 ) ; 12 p r i n t f ( " %E\ E \ E n " , 1234567 . 89 ) ; 13 p r i n t f ( " %f \ n " , 1234567 . 89 ) ; 14 p r i n t f ( " %g \ n " , 1234567 . 89 8 ) ; 15 p r i n t f ( " %G \ n " , 1234567 . 89 ) ; 16 17 r e t u r n 0 ; / * i nd i ca t e s s u c c es s f u l t e r mi na n t i on o * / 18 19 } / * end ma i n * / Program Output 1.234568e+006 1.234568e+006 -1.234568e+006 1.234568E+006 1234567.890000 1.23457e+006 1.23457E+006
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 13 9.6
Printing Strings and Characters • c – Prints char argument
– Cannot be used to print the first character of a string • s
– Requires a pointer to c har as an argument
– Prints characters until NULL (' \ 0' ) encountered
– Cannot print a char argument • Remember
– Single quotes for character constants (' z' )
– Double quotes for strings " z" (which actually contains two characters, ' z' and ' \ 0' )
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 / / * * Fi F i g 9. . 5: : f f i i g09_05 0 c c * * / / 14 2 / / * * Pr P r i i nt t i i ng s s t t r r i i ng n s and c c har r ac a c t t er r s * * / / Ou O t u l t iln i e n 3 #i # i nc c l l ude d < s t t di i o. o . h> 4 fig09_05.c 5 i i nt t mai i n( ( ) ) 6 { { 7 c har a r c c har r act t er r = = ' ' A' A ' ; ; / / * * i i ni i t t i i al l i i z z e c c har r * * / / 8 c har a r s s t t r r i i ng n [ [ ] ] = = " " Th T i h i s i i s a s s t t r r i i ng n " " ; / / * * i i ni i t t i i al a l i i ze c ha h r r ar r r r ay a y * * / / 9 c ons n s t t c c har * * s t t r r i i ng n Pt P t r r = = " " Th T i h i s i i s al l s o o a s s t t r r i i ng" " ; ; / / * * c har a r poi i nt n t er r * * / / 10 11 pr r i i nt t f f ( ( " " %c\ \ n" " , , c c har r ac a c t t er r ) ) ; ; 12 pr r i i nt t f f ( ( " " %s\ \ n" " , , " " Th T i h i s i i s a s s t t r r i i ng n " " ) ) ; ; 13 pr r i i nt t f f ( ( " " %s\ \ n" " , , s s t t r r i i ng n ) ) ; ; 14 pr r i i nt t f f ( ( " " %s\ \ n" " , , s s t t r r i i ng n Pt t r r ) ) ; ; 15 16 r r et t ur r n 0; ; / / * * i i ndi i cat t es s s uc c es e s s s f f ul l t t er r mi i na n t t i i on o * * / / 17 18 } } / / * * end d mai i n * * / / A Th T i s i s a s t r i ng n Th T i s i s a s t r i ng n Th T i s i s a l s o a s t r i ng n
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 15 9.7
Other Conversion Specifiers • p
– Displays pointer value (address) • n
– Stores number of characters already output by current pr i nt f statement
– Takes a pointer to an integer as an argument
– Nothing printed by a %n specification
– Every pr i nt f call returns a value
• Number of characters output
• Negative number if error occurs • % – Prints a percent sign – %%
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 9.7
Other Conversion Specifiers Conversion specifier Description p
Display a pointer value in an implementation-defined manner. n
Store the number of characters already output in the current
pr i nt f statement. A pointer to an integer is supplied as the
corresponding argument. Nothing is displayed. %
Display the percent character. Fig. 9.6 Other conversion specifiers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 1 / * Fi F i F g g g 9 . 7 : f i g0 g 9_ 9 07 . c * / 17 2 / * U s i ng n t he e e p , n , and % co nv n v n e r s i on s pec i f i er e r e s * / Ou O t u l t iln i e n 3 #i # i # n c l ud u e < s < t d i o. o . o h> > 4 fig09_07.c (1 of 2) 5 i n t mai a i a n ( ) 6 { 7 i n t * pt p t p r ; / * de f i ne n po i nt n t n e r t o i nt n t n * / 8 i n t x = = = 12345 ; / * i ni n i n t i a l i z e i n t x * / 9 i n t y; / * de f i ne n i n t y * / 10 11 p t r = &x & ; / * a s s i gn g add r es s o f x t o p t r * / 12 p r i n t f ( " Th T e h v a l u e e e o f pt p t p r i s %p \ n " , p t r ) ; 13 p r i n t f ( " Th T e h add r es e s e s o f x i s %p \ n \ n " , &x ) ; 14 15 p r i n t f ( " To T t o t a l c ha r ac t e r s p r i n t ed o n n n t h i s l i ne : %n " , &y & ) ; 16 p r i n t f ( " %d\ \ n\ \ n" " , y ) ; 17 18 y = = = p r i nt n t n f ( " Th T i s l i ne n has a s a 28 c ha r ac t e r s \ n " ) ; 19 p r i n t f ( " %d c ha r ac t e r s wer e r e e p r i nt n t n ed \ n \ n " , y ) ; 20 21 p r i n t f ( " P r i n t i ng a % % i n n n a f o r mat a t a c on t r o l s t r i ng n \ n " ) ; 22 23 r e t u r n 0 ; / * i nd i ca t es s uc c es e s e s f u l t e r mi na n t i on o * / 24 25 } / * end mai a i a n * /
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18
The value of ptr is 0012FF78 Ou O t u l t iln i e n
The address of x is 0012FF78
Total characters printed on this line: 38 Program Output This line has 28 characters 28 characters were printed
Printing a % in a format control string
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 19 9.8
Printing with Field Widths and Precisions • Field width
– Size of field in which data is printed
– If width larger than data, default right justified
• If field width too small, increases to fit data
• Minus sign uses one character position in field
– Integer width inserted between %and conversion specifier – %4d – field width of 4
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 9.8
Printing with Field Widths and Precisions • Precision
– Meaning varies depending on data type – Integers (default 1)
• Minimum number of digits to print
– If data too small, prefixed with zeros – Floating point
• Number of digits to appear after decimal (e and f )
– For g – maximum number of significant digits – Strings
• Maximum number of characters to be written from string – Format
• Use a dot (. ) then precision number after % %. 3f
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 21 9.8
Printing with Field Widths and Precisions • Field width and precision – Can both be specified • %wi dt h. pr ec i s i on %5. 3f
– Negative field width – left justified
– Positive field width – right justified – Precision must be positive
– Can use integer expressions to determine field width and precision values
• Place an asterisk (* ) in place of the field width or precision
– Matched to an i nt argument in argument list • Example:
pr i nt f ( " %* . * f " , 7, 2, 98. 736 ) ;
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 / / * * Fi F i g g 9. . 8: : f f i i g0 g 9_08. . c c * * / / 22 2 / / * * Pr P r i i nt t i i ng g i i nt t eger r s s r r i i ght t - - j j us u s t t i i f f i i ed e * * / / Ou O t u l t iln i e n 3 #i i nc n c l l ude < s t t di i o. o . h> 4 fig09_08.c 5 i i nt n t mai i n( n ( ) ) 6 { { 7 pr r i i nt n t f f ( ( " " %4d\ \ n" " , , 1 ) ; 8 pr r i i nt n t f f ( ( " " %4d\ \ n" " , , 12 ) ) ; ; 9 pr r i i nt n t f f ( ( " " %4d\ \ n" " , , 123 ) ) ; ; 10 pr r i i nt n t f f ( ( " " %4d\ \ n" " , , 1234 ) ) ; ; 11 pr r i i nt n t f f ( ( " " %4d\ \ n\ \ n" " , , 12345 4 ) ) ; ; 12 13 pr r i i nt n t f f ( ( " " %4d\ \ n" " , , - - 1 ) ) ; ; 14 pr r i i nt n t f f ( ( " " %4d\ \ n" " , , - - 12 ) ) ; ; 15 pr r i i nt n t f f ( ( " " %4d\ \ n" " , , - - 123 ) ) ; ; 16 pr r i i nt n t f f ( ( " " %4d\ \ n" " , , - - 1234 ) ; 17 pr r i i nt n t f f ( ( " " %4d\ \ n" " , , - - 12345 ) ) ; ; 18 19 r r et t ur r n 0; ; / / * * i i nd n i i c at a t es s s uc u c c c es s s f f ul l t t er r m mi na n t t i i on o * * / / 20 21 } } / / * * end mai i n * * / /
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 23 1 Ou O t u l t iln i e n 12 123 123 2 4 1 Program Output 123 2 45 - 1 - 12 - 12 1 3 - 12 1 34 - 12 1 345 4
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 / * Fi g 9 . 9 : f i g 0 9 _ 0 9 . c * / 24
2 / * Us i ng p r e c i s i on wh i l e p r i n t i n g i nt e g e r s , Ou O t u l t iln i e n
3 f l o at i n g- po i nt n u mb e r s , a nd s t r i n g s * /
4 #i n c l u de < s t d i o. h > fig09_09.c 5 6 i n t ma i n ( ) 7 {
8 i n t i = 87 3; / * i ni t i a l i z e i nt i * /
9 d o u b l e f = 12 3 . 9 4 5 3 6 ; / * i n i t i a l i z e d o u bl e f * /
10 c h a r s [ ] = " Hap p y Bi r t hd a y " ; / * i ni t i a l i z e c h a r a r r a y s * / 11
12 pr i n t f ( " Us i ng p r ec i s i on f o r i n t eg e r s \ n " ) ;
13 pr i n t f ( " \ t %. 4d \ n \ t %. 9 d \ n \ n " , i , i ) ; 14
15 pr i n t f ( " Us i ng p r ec i s i on f o r f l o at i n g- po i nt n u mb e r s \ n " ) ;
16 pr i n t f ( " \ t %. 3f \ n \ t %. 3 e \ n \ t %. 3 g \ n \ n " , f , f , f ) ; 17
18 pr i n t f ( " Us i ng p r ec i s i on f o r s t r i n g s \ n" ) ;
19 pr i n t f ( " \ t %. 11 s \ n" , s ) ; 20
21 r et u r n 0 ; / * i n d i c a t e s s uc c e s s f u l t er mi n at i o n * / 22
23 } / * en d ma i n * /
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12