CS2_44_C_Part3

CS2_44_C_Part3 - CS2 Module 44 Category: Elements of C...

Info iconThis preview shows pages 1–18. Sign up to view the full content.

View Full Document Right Arrow Icon
CS2 Module 44 Category: Elements of C Topic: Part III Objectives Preprocessor include define I/O
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS 2 Introduction to Object Oriented Programming Module 44 Elements of C Part III
Background image of page 2
Preprocessing Compilation begins by moving the C source files through a preprocessor--a simple text substitution device. One can designate strings for substitution with the expression: #define <expression> <substitute> #define <expression> <substitute> #include <stdio.h> #define GREETING "Hello World" int main ( void ) { printf (GREETING); } int main ( void ) { printf ("Hello World"); }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The C preprocessor has many features--and pitfalls. #define g(y) get_index(y) g(z) #define g (y) get_index (y) g(z) A Tale of Two Expansions get_index(z) (y) get_index (y) (z) Notice the extra white space This is known as "macro substitution"
Background image of page 4
The Power of Parens #define DIVIDE(x, y) x/y DIVIDE(3, 3+4) 3/3+4 #define DIVIDE(x, y) (x)/(y) DIVIDE (3, 3+4) (3)/(3+4) Inputs Results
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
More Substitution #define square(x) x * x #define square(x) x * x What's wrong with this? It could be called thus: square (z + 4) square (z + 4) Resulting: z + 4 * z + 4 z + 4 * z + 4 And not: (z+4) * (z+4) (z+4) * (z+4)
Background image of page 6
Common Sense Preprocessing 1 Watch extra white space, as shown in the previous examples. 2 Liberally use parentheses to protect order of precedence
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Questions?
Background image of page 8
And now…. I/O in C Streams -- What are they? Reading from the keyboard -- safety first More printf fun -- more than meets the eye Reading/writing files Misc. C Trivia -- Enough already!
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
First there was I/O. .. ...then computers!
Background image of page 10
Input 1 Jacquard uses "punched cards" to control loom Hollerith at MIT develops punched card technology to handle 1890 census data CTR (Calculating, Tabulating, and Recording Company (1911) IBM) IBM (1924) Fixed length records
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The formerly ubiquitous punched card
Background image of page 12
Input 2 Telephone Telegraph Wireless Teletype Paper Tape Paper tape wastes no space. It appears to be a stream of data arriving as input or leaving as punched output.
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Streaming Input Paper tape Input from keyboard Input from modem Others?
Background image of page 14
Well, yes. .. Even devices like disk drives which inherently are not "stream" oriented devices can be made to act that way through software techniques.
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Buffers? Whether a device is inherently stream oriented (like a keyboard) or being forced to "emulate" stream behavior doesn't matter. Modern computer systems store stream data in buffers. When you ask for some it gets it out of the buffer. This normally solves a speed mismatch problem and makes things much more efficient. Almost anything can be used as a stream, or a source of input: a keyboard, a network connection, a file--even a string in memory. Stream Stream source source H E L L O W O Buffer Buffer (limited size) (limited size) Program Program using stream stream
Background image of page 16
By default A C program will have several "streams" defined by default.
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 18
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 63

CS2_44_C_Part3 - CS2 Module 44 Category: Elements of C...

This preview shows document pages 1 - 18. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online