32
Computer Science: A Structured Programming Approach Using C 1 -7 Input/Output Although our programs have implicitly shown Although our programs have implicitly shown how to print messages, we have not formally how to print messages, we have not formally discussed how we use C facilities to input and discussed how we use C facilities to input and output data. We devote two chapters, Chapter 7 output data. We devote two chapters, Chapter 7 and 13, to fully explain the C input/output and 13, to fully explain the C input/output facilities and how to use them. In this section, facilities and how to use them. In this section, we describe simple input and output formatting. we describe simple input and output formatting. Streams Formatting Input/Output Topics discussed in this section: Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Embed Size (px)

Citation preview

Page 1: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 1

2-7 Input/Output

Although our programs have implicitly shown how to Although our programs have implicitly shown how to print messages, we have not formally discussed how we print messages, we have not formally discussed how we use C facilities to input and output data. We devote two use C facilities to input and output data. We devote two chapters, Chapter 7 and 13, to fully explain the C chapters, Chapter 7 and 13, to fully explain the C input/output facilities and how to use them. In this input/output facilities and how to use them. In this section, we describe simple input and output formatting.section, we describe simple input and output formatting.

StreamsFormatting Input/Output

Topics discussed in this section:Topics discussed in this section:

Page 2: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 2

A terminal keyboard and monitor can be associated only with a text stream. A keyboard is a source

for a text stream; a monitor is a destination for a text stream.

NoteNote

Page 3: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 3

FIGURE 2-15 Stream Physical Devices

Page 4: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 4

FIGURE 2-16 Output Formatting Concept

Page 5: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 5

FIGURE 2-17 Output Stream Formatting Example

Page 6: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 6

FIGURE 2-18 Conversion Specification

Page 7: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 7

Table 2-10 Format Codes for Output

Page 8: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 8

Table 2-11 Flag Formatting Options

Page 9: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 9

FIGURE 2-19 Formatting Text from an Input Stream

Page 10: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 10

FIGURE 2-20 Input Stream Formatting Example

Page 11: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 11

FIGURE 2-21 Conversion Specification

Page 12: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 12

scanf requires variable addresses in the address list.

NoteNote

Page 13: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 13

Table 2-12 scanf Rules

Page 14: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 14

2-8 Programming Examples

In this section, we show some programming example to In this section, we show some programming example to emphasize the ideas and concepts we have discussed emphasize the ideas and concepts we have discussed about input/output.about input/output.

Page 15: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 15

PROGRAM 2-4 A Program That Prints “Nothing!”

Page 16: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 16

PROGRAM 2-5 Demonstrate Printing Boolean Constants

Page 17: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 17

PROGRAM 2-5 Demonstrate Printing Boolean Constants (continued)

Page 18: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 18

PROGRAM 2-6 Print Value of Selected Characters

Page 19: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 19

PROGRAM 2-6 Print Value of Selected Characters (continued)

Page 20: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 20

PROGRAM 2-6 Print Value of Selected Characters (continued)

Page 21: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 21

PROGRAM 2-6 Print Value of Selected Characters (continued)

Page 22: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 22

PROGRAM 2-7 Calculate a Circle’s Area and Circumference

Page 23: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 23

PROGRAM 2-7 Calculate a Circle’s Area and Circumference (continued)

Page 24: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 24

FIGURE 2-22 Output Specifications for Inventory Report

Page 25: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 25

PROGRAM 2-8 A Sample Inventory Report

Page 26: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 26

PROGRAM 2-8 A Sample Inventory Report (continued)

Page 27: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 27

FIGURE 2-23 Complex Number Attributes

Page 28: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 28

PROGRAM 2-9 Print Complex Number Attributes

Page 29: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 29

PROGRAM 2-9 Print Complex Number Attributes (continued)

Page 30: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 30

PROGRAM 2-10 Complex Number Arithmetic

Page 31: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 31

PROGRAM 2-10 Complex Number Arithmetic (continued)

Page 32: Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have

Computer Science: A Structured Programming Approach Using C 32

PROGRAM 2-10 Complex Number Arithmetic (continued)