C How to Program 7th Edition Deitel Solutions Manual instant download
C How to Program 7th Edition Deitel Solutions Manual instant download
https://testbankfan.com/product/c-how-to-program-7th-edition-
deitel-solutions-manual/
https://testbankfan.com/product/c-how-to-program-7th-edition-
deitel-test-bank/
https://testbankfan.com/product/c-how-to-program-10th-edition-
deitel-solutions-manual/
https://testbankfan.com/product/visual-c-how-to-program-6th-
edition-deitel-solutions-manual/
https://testbankfan.com/product/c-how-to-program-10th-edition-
deitel-test-bank/
Visual C 2012 How to Program 5th Edition Deitel
Solutions Manual
https://testbankfan.com/product/visual-c-2012-how-to-program-5th-
edition-deitel-solutions-manual/
https://testbankfan.com/product/c-how-to-program-late-objects-
version-7th-edition-deitel-test-bank/
https://testbankfan.com/product/visual-c-how-to-program-6th-
edition-deitel-test-bank/
https://testbankfan.com/product/visual-c-how-to-program-6th-
edition-deitel-test-bank-2/
https://testbankfan.com/product/c-how-to-program-early-objects-
version-9th-edition-deitel-solutions-manual/
9 C Formatted Input/Output
Objectives
In this chapter, you’ll:
■ Use input and output
streams.
■ Use all print formatting
capabilities.
■ Use all input formatting
capabilities.
■ Print with field widths and
precisions.
■ Use formatting flags in the
printf format control
string.
■ Output literals and escape
sequences.
■ Format input using scanf.
Self-Review Exercises 565
Self-Review Exercises
9.1 Fill in the blanks in each of the following:
a) All input and output is dealt with in the form of .
ANS: streams.
b) The stream is normally connected to the keyboard.
ANS: standard input.
c) The stream is normally connected to the computer screen.
ANS: standard output.
d) Precise output formatting is accomplished with the function.
ANS: printf.
e) The format control string may contain , , ,
and .
ANS: Conversion specifiers, flags, field widths, precisions, literal characters.
f) The conversion specifier or may be used to output a signed
decimal integer.
ANS: d, i.
g) The conversion specifiers , and are used to dis-
play unsigned integers in octal, decimal and hexadecimal form, respectively.
ANS: o, u, x (or X).
h) The modifiers and are placed before the integer conversion
specifiers to indicate that short or long integer values are to be displayed.
ANS: h, l.
i) The conversion specifier is used to display a floating-point value in expo-
nential notation.
ANS: e (or E).
j) The modifier is placed before any floating-point conversion specifier to in-
dicate that a long double value is to be displayed.
ANS: L.
k) The conversion specifiers e, E and f are displayed with digits of precision
to the right of the decimal point if no precision is specified.
ANS: 6.
l) The conversion specifiers and are used to print strings and
characters, respectively.
ANS: s, c.
m) All strings end in the character.
ANS: NULL ('\0').
n) The field width and precision in a printf conversion specifier can be controlled with
integer expressions by substituting a(n) for the field width or for the pre-
cision and placing an integer expression in the corresponding argument of the argument
list.
ANS: asterisk (*).
o) The flag causes output to be left justified in a field.
ANS: - (minus).
p) The flag causes values to be displayed with either a plus sign or a minus
sign.
ANS: p) + (plus).
q) Precise input formatting is accomplished with the function.
ANS: scanf.
566 Chapter 9 C Formatted Input/Output
r) A(n) is used to scan a string for specific characters and store the characters
in an array.
ANS: scan set.
s) The conversion specifier can be used to input optionally signed octal, dec-
imal and hexadecimal integers.
ANS: i.
t) The conversion specifiers can be used to input a double value.
ANS: le, lE, lf, lg or lG.
u) The is used to read data from the input stream and discard it without as-
signing it to a variable.
ANS: assignment suppression character (*).
v) A(n) can be used in a scanf conversion specifier to indicate that a specific
number of characters or digits should be read from the input stream.
ANS: field width.
9.2 Find the error in each of the following and explain how the error can be corrected.
a) The following statement should print the character 'c'.
printf( "%s\n", 'c' );
ANS: Error: Conversion specifier s expects an argument of type pointer to char.
Correction: To print the character 'c', use the conversion specifier %c or change 'c'
to "c".
b) The following statement should print 9.375%.
printf( "%.3f%", 9.375 );
ANS: Error: Trying to print the literal character % without using the conversion specifier %%.
Correction: Use %% to print a literal % character.
c) The following statement should print the first character of the string "Monday".
printf( "%c\n", "Monday" );
ANS: Error: Conversion specifier c expects an argument of type char.
Correction: To print the first character of "Monday" use the conversion specifier %1s.
d) printf( ""A string in quotes"" );
ANS: Error: Trying to print the literal character " without using the \" escape sequence.
Correction: Replace each quote in the inner set of quotes with \".
e) printf( %d%d, 12, 20 );
ANS: Error: The format control string is not enclosed in double quotes.
Correction: Enclose %d%d in double quotes.
f) printf( "%c", "x" );
ANS: Error: The character x is enclosed in double quotes.
Correction: Character constants to be printed with %c must be enclosed in single
quotes.
g) printf( "%s\n", 'Richard' );
ANS: Error: The string to be printed is enclosed in single quotes.
Correction: Use double quotes instead of single quotes to represent a string.
9.3 Write a statement for each of the following:
a) Print 1234 right justified in a 10-digit field.
ANS: printf( "%10d\n", 1234 );
b) Print 123.456789 in exponential notation with a sign (+ or -) and 3 digits of precision.
ANS: printf( "%+.3e\n", 123.456789 );
c) Read a double value into variable number.
ANS: scanf( "%lf", &number );
d) Print 100 in octal form preceded by 0.
ANS: printf( "%#o\n", 100 );
Exercises 567
Exercises
9.4 Write a printf or scanf statement for each of the following:
a) Print unsigned integer 40000 left justified in a 15-digit field with 8 digits.
ANS: printf( “%-15.8u”, ( unsigned int ) 40000 );
b) Read a hexadecimal value into variable hex.
ANS: scanf( “%x”, &hex );
c) Print 200 with and without a sign.
ANS: printf( “%+d %d\n”, 200, 200 );
d) Print 100 in hexadecimal form preceded by 0x.
ANS: printf( %#x\n”, 100 );
e) Read characters into array s until the letter p is encountered.
ANS: scanf( “%[^p]”, s );
f) Print 1.234 in a 9-digit field with preceding zeros.
ANS: printf( “%09.3f\n”, 1.234 );
g) Read a time of the form hh:mm:ss, storing the parts of the time in the integer variables
hour, minute and second. Skip the colons (:) in the input stream. Use the assignment
suppression character.
ANS: scanf( “%d%*c%d%*c%d”, &hour, &minute, &second );
h) Read a string of the form "characters" from the standard input. Store the string in
character array s. Eliminate the quotation marks from the input stream.
ANS: scanf( “\”%[^\”]”, s );
i) Read a time of the form hh:mm:ss, storing the parts of the time in the integer variables
hour, minute and second. Skip the colons (:) in the input stream. Do not use the as-
signment-suppression character.
ANS: scanf( “%d:%d:%d:”, &hour, &minute, &second );
9.5 Show what is printed by each of the following statements. If a statement is incorrect, indi-
cate why.
a) printf( "%-10d\n", 10000 );
ANS: 10000
b) printf( "%c\n", "This is a string" );
ANS: A string cannot be printed with the %c specifier.
c) printf( "%*.*lf\n", 8, 3, 1024.987654 );
ANS: 1024.988
d) printf( "%#o\n%#X\n%#e\n", 17, 17, 1008.83689 );
ANS:
021
0X11
1.008837e+03
568 Chapter 9 C Formatted Input/Output
to input and print the values. Test the program with the following sets of input data:
10 10
-10 -10
010 010
0x10 0x10
ANS:
9.8 (Printing Numbers in Various Field Widths) Write a program to test the results of printing
the integer value 12345 and the floating-point value 1.2345 in various size fields. What happens
when the values are printed in fields containing fewer digits than the values?
ANS:
12345
12345
12345
1.234500
1.234500
1.234500
570 Chapter 9 C Formatted Input/Output
9.9 (Rounding Floating-Point Numbers) Write a program that prints the value 100.453627
rounded to the nearest digit, tenth, hundredth, thousandth and ten-thousandth.
ANS:
100
100.5
100.45
100.454
100.4536
9.10 (Temperature Conversions) Write a program that converts integer Fahrenheit temperatures
from 0 to 212 degrees to floating-point Celsius temperatures with 3 digits of precision. Perform the
calculation using the formula
celsius = 5.0 / 9.0 * ( fahrenheit - 32 );
to perform the calculation. The output should be printed in two right-justified columns of 10
characters each, and the Celsius temperatures should be preceded by a sign for both positive and
negative values.
ANS:
Fahrenheit Celsius
0 -17.778
1 -17.222
2 -16.667
3 -16.111
4 -15.556
5 -15.000
6 -14.444
7 -13.889
.
.
.
204 +95.556
205 +96.111
206 +96.667
207 +97.222
208 +97.778
209 +98.333
210 +98.889
211 +99.444
212 +100.000
9.11 (Escape Sequences) Write a program to test the escape sequences \', \", \?, \\, \a, \b, \n,
\r and \t. For the escape sequences that move the cursor, print a character before and after printing
the escape sequence so it’s clear where the cursor has moved.
ANS:
9.12 (Printing a Question Mark) Write a program that determines whether ? can be printed as
part of a printf format control string as a literal character rather than using the \? escape sequence.
ANS:
9.13 (Reading an Integer with Each scanf Conversion Specifier) Write a program that inputs the
value 437 using each of the scanf integer conversion specifiers. Print each input value using all the
integer conversion specifiers.
ANS:
20
21 // print each of the 5 values
22 printf( "%s\n%d %i %o %u %x\n\n", s[ loop ], array[ loop ],
23 array[ loop ], array[ loop ], array[ loop ], array[ loop ] );
24 } // end for
25 } // end main
Enter the value 437 five times: 437 437 437 437 437
Read with %d:
437 437 665 437 1b5
9.14 (Outputting a Number with the Floating-Point Conversion Specifiers) Write a program
that uses each of the conversion specifiers e, f and g to input the value 1.2345. Print the values of
each variable to prove that each conversion specifier can be used to input this same value.
ANS:
9.15 (Reading Strings in Quotes) In some programming languages, strings are entered surround-
ed by either single or double quotation marks. Write a program that reads the three strings suzy,
"suzy" and 'suzy'. Are the single and double quotes ignored by C or read as part of the string?
ANS:
9.16 (Printing a Question Mark as a Character Constant) Write a program that determines
whether ? can be printed as the character constant '?' rather than the character constant escape se-
quence '\?' using conversion specifier %c in the format control string of a printf statement.
ANS:
9.17 (Using %g with Various Precisions) Write a program that uses the conversion specifier g to
output the value 9876.12345. Print the value with precisions ranging from 1 to 9.
ANS:
KIESELBACHER CHLORATSPRENGSTOFF.
See MIEDZIANKIT.
KINETIT.—A German explosive made by gelatinising nitro-
cellulose with nitro-benzene, and incorporating it with potassium
nitrate and chlorate. It is somewhat sensitive to blows, etc. Early
samples contained also antimony sulphide which rendered them
decidedly dangerous.
KIWIT.—A German chlorate explosive introduced during the
War. It contains not more than 77 per cent. of sodium or potassium
chlorate, carbon carriers such as paraffin, naphthalene, vaseline,
meal or oil, also not more than 15 per cent. of liquid trinitro-toluene,
and may contain dinitro-toluene, dinitro-naphthalene, sodium
chloride and not more than 4 per cent. of guncotton.
KOHLENKARBONIT. See CARBONITE.
KOLAX.—A coal-mine explosive of the Carbonite type formerly
on the Permitted List, made by Curtis’s and Harvey—
Nitroglycerine 25
Potassium nitrate 26
Barium nitrate 5
Wood meal 34
Starch 10