0% found this document useful (0 votes)
14 views

Slide11 - 2

Uploaded by

bewahig513
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Slide11 - 2

Uploaded by

bewahig513
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

Week 12

C Formatted Input/Output
C How to Program, 7/e

© 2016 Pearson Education, Ltd. All rights reserved.


OBJECTIVES
• Use input and output streams.
• Use print formatting capabilities.
• Use input formatting capabilities.
• Print integers, floating-point numbers, strings and characters.
• Print with field widths and precisions.
• Use formatting flags in the printf format control string.
• Output literals and escape sequences.
• Read formatted input using scanf.
9.1 Introduction
• An important part of the solution to any
problem is the presentation of the results.
• In this chapter, we discuss in depth the
formatting features of scanf and printf.
• These functions input data from the standard
input stream and output data to the standard
output stream.
• Include the header <stdio.h> in programs
that call these functions.

© 2016 Pearson Education, Ltd. All rights reserved.


9.3 Formatting Output with printf

• Precise output formatting is accomplished with


printf.
• Every printf call contains a format control
string that describes the output format.
• The format control string consists of conversion
specifiers, flags, field widths, precisions and
literal characters.
• Together with the percent sign (%), these form
conversion specifications.
© 2016 Pearson Education, Ltd. All rights reserved.
9.4 Printing Integers

• An integer is a whole number, such as 776, 0


or –52, that contains no decimal point.
• Integer values are displayed in one of several
formats.
• Figure 9.1 describes the integer conversion
specifiers.

© 2016 Pearson Education, Ltd. All rights reserved.


© 2016 Pearson Education, Ltd. All rights reserved.
Number

System Base Digits

Binary 2 01

Octal 8 01234567

Decimal 10 0123456789

Hexadecimal 16 0123456789ABCDEF
9.4 Printing Integers (Cont.)

• Figure 9.2 prints an integer using each of the


integer conversion specifiers.
• Only the minus sign prints; plus signs are
normally suppressed.
• Also, the value -455, when read by %u (line
15), is interpreted as an unsigned value
4294966841.

© 2016 Pearson Education, Ltd. All rights reserved.


© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
9.8 Printing with Field Widths and Precision

• The exact size of a field in which data is printed is specified


by a field width.
• If the field width is larger than the data being printed, the
data will normally be right justified within that field.
• An integer representing the field width is inserted between
the percent sign (%) and the conversion specifier (e.g., %4d).
• Figure 9.8 prints two groups of five numbers each, right
justifying those containing fewer digits than the field width.
• The field width is increased to print values wider than the
field.
• Note that the minus sign for a negative value uses one
character position in the field width.
• Field widths can be used with all conversion specifiers.

© 2016 Pearson Education, Ltd. All rights reserved.


© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
9.9 Using Flags in the printf Format Control String

• Function printf also provides flags to


supplement its output formatting capabilities.
• Five flags are available for use in format control
strings (Fig. 9.10).
• To use a flag in a format control string, place
the flag immediately to the right of the percent
sign.
• Several flags may be combined in one
conversion specifier.
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
9.9 Using Flags in the printf Format Control String
(Cont.)

• Figure 9.11 demonstrates right justification


and left justification of a string, an integer, a
character and a floating-point number.

© 2016 Pearson Education, Ltd. All rights reserved.


© 2016 Pearson Education, Ltd. All rights reserved.
9.9 Using Flags in the printf Format Control String
(Cont.)

• Figure 9.12 prints a positive number and a


negative number, each with and without the +
flag.
• The minus sign is displayed in both cases, but
the plus sign is displayed only when the + flag
is used.

© 2016 Pearson Education, Ltd. All rights reserved.


© 2016 Pearson Education, Ltd. All rights reserved.
9.9 Using Flags in the printf Format Control String
(Cont.)

• Figure 9.13 prefixes a space to the positive


number with the space flag.
• This is useful for aligning positive and
negative numbers with the same number of
digits.
• The value -547 is not preceded by a space in
the output because of its minus sign.

© 2016 Pearson Education, Ltd. All rights reserved.


© 2016 Pearson Education, Ltd. All rights reserved.
9.9 Using Flags in the printf Format Control String
(Cont.)

• Figure 9.14 uses the # flag to prefix 0 to the


octal value and 0x and 0X to the hexadecimal
values, and to force the decimal point on a
value printed with g.

© 2016 Pearson Education, Ltd. All rights reserved.


© 2016 Pearson Education, Ltd. All rights reserved.
9.9 Using Flags in the printf Format Control String
(Cont.)

• Figure 9.15 combines the + flag and the 0


(zero) flag to print 452 in a 9-space field with
a + sign and leading zeros, then prints 452
again using only the 0 flag and a 9-space field.

© 2016 Pearson Education, Ltd. All rights reserved.


© 2016 Pearson Education, Ltd. All rights reserved.
9.10 Printing Literals and Escape Sequences

• Most literal characters to be printed in a printf statement


can simply be included in the format control string.
• However, there are several “problem” characters, such as
the quotation mark (") that delimits the format control
string itself.
• Various control characters, such as newline and tab, must be
represented by escape sequences.
• An escape sequence is represented by a backslash (\),
followed by a particular escape character.
• Figure 9.16 lists the escape sequences and the actions they
cause.
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
9.11 Reading Formatted Input with scanf

• Precise input formatting can be accomplished with


scanf.
• Every scanf statement contains a format control
string that describes the format of the data to be input.
• The format control string consists of conversion
specifiers and literal characters.
• Function scanf has the following input formatting
capabilities:
– Inputting all types of data.
– Inputting specific characters from an input stream.
– Skipping specific characters in the input stream.
© 2016 Pearson Education, Ltd. All rights reserved.
9.11 Reading Formatted Input with scanf (Cont.)

• Figure 9.17 summarizes the conversion


specifiers used to input all types of data.
• The remainder of this section provides
programs that demonstrate reading data with
the various scanf conversion specifiers.
• Note that the d and i conversion specifiers
have different meanings for input with
scanf, whereas they’re interchangeable for
output with printf.
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
9.11 Reading Formatted Input with scanf (Cont.)

• Figure 9.18 reads integers with the various


integer conversion specifiers and displays the
integers as decimal numbers.
• Conversion specifier %i can input decimal,
octal and hexadecimal integers.

© 2016 Pearson Education, Ltd. All rights reserved.


© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
9.11 Reading Formatted Input with scanf (Cont.)

• When inputting floating-point numbers, any of the


floating-point conversion specifiers e, E, f, g or G
can be used.
• Figure 9.19 reads three floating-point numbers, one
with each of the three types of floating conversion
specifiers, and displays all three numbers with
conversion specifier f.
• The program output confirms the fact that floating-
point values are imprecise—this is highlighted by
the third value printed.
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
9.11 Reading Formatted Input with scanf (Cont.)

• Characters and strings are input using the


conversion specifiers c and s, respectively.
• Figure 9.20 prompts the user to enter a string.
• The program inputs the first character of the
string with %c and stores it in the character
variable x, then inputs the remainder of the
string with %s and stores it in character array
y.

© 2016 Pearson Education, Ltd. All rights reserved.


© 2016 Pearson Education, Ltd. All rights reserved.
9.11 Reading Formatted Input with scanf (Cont.)

• A sequence of characters can be input using a scan set.


• A scan set is a set of characters enclosed in square brackets,
[], and preceded by a percent sign in the format control
string.
• A scan set scans the characters in the input stream, looking
only for those characters that match characters contained in
the scan set.
• Each time a character is matched, it’s stored in the scan set’s
corresponding argument—a pointer to a character array.
• The scan set stops inputting characters when a character
that’s not contained in the scan set is encountered.

© 2016 Pearson Education, Ltd. All rights reserved.


9.11 Reading Formatted Input with scanf (Cont.)

• If the first character in the input stream does


not match a character in the scan set, the array
is not modified.
• Figure 9.21 uses the scan set [aeiou] to
scan the input stream for vowels.

© 2016 Pearson Education, Ltd. All rights reserved.


• Notice that the first seven letters of the input are read.
• The eighth letter (h) is not in the scan set and therefore the scanning is
terminated.

© 2016 Pearson Education, Ltd. All rights reserved.


9.11 Reading Formatted Input with scanf (Cont.)

• The scan set can also be used to scan for characters not
contained in the scan set by using an inverted scan set.
• To create an inverted scan set, place a caret (^) in the
square brackets before the scan characters.
• This causes characters not appearing in the scan set to
be stored.
• When a character contained in the inverted scan set is
encountered, input terminates.
• Figure 9.22 uses the inverted scan set [^aeiou] to
search for consonants—more properly to search for
“nonvowels.”
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
9.11 Reading Formatted Input with scanf (Cont.)

• A field width can be used in a scanf


conversion specifier to read a specific number
of characters from the input stream.
• Figure 9.23 inputs a series of consecutive
digits as a two-digit integer and an integer
consisting of the remaining digits in the input
stream.

© 2016 Pearson Education, Ltd. All rights reserved.


© 2016 Pearson Education, Ltd. All rights reserved.
9.11 Reading Formatted Input with scanf (Cont.)

• Often it’s necessary to skip certain characters in the


input stream.
• For example, a date could be entered as
• 11-10-1999
• Each number in the date needs to be stored, but the
dashes that separate the numbers can be discarded.
• To eliminate unnecessary characters, include them
in the format control string of scanf (whitespace
characters—such as space, newline and tab—skip
all leading whitespace).
© 2016 Pearson Education, Ltd. All rights reserved.
9.11 Reading Formatted Input with scanf (Cont.)

• For example, to skip the dashes in the input, use the


statement
• scanf("%d-%d-%d", &month, &day, &year);
• Although this scanf does eliminate the dashes in
the preceding input, it’s possible that the date could
be entered as
• 10/11/1999
• In this case, the preceding scanf would not
eliminate the unnecessary characters.
• For this reason, scanf provides the assignment
suppression character *.
© 2016 Pearson Education, Ltd. All rights reserved.
9.11 Reading Formatted Input with scanf (Cont.)

• This character enables scanf to read any type of data from


the input and discard it without assigning it to a variable.
• Figure 9.24 uses the assignment suppression character in the
%c conversion specifier to indicate that a character
appearing in the input stream should be read and discarded.
• Only the month, day and year are stored.
• The values of the variables are printed to demonstrate that
they’re in fact input correctly.
• The argument lists for each scanf call do not contain
variables for the conversion specifiers that use the
assignment suppression character.
• The corresponding characters are simply discarded.

© 2016 Pearson Education, Ltd. All rights reserved.


© 2016 Pearson Education, Ltd. All rights reserved.

You might also like