Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exam 4 review Copyright © 2008 W. W. Norton & Company.

Similar presentations


Presentation on theme: "Exam 4 review Copyright © 2008 W. W. Norton & Company."— Presentation transcript:

1 Exam 4 review Copyright © 2008 W. W. Norton & Company.
All rights reserved.

2 Introduction Directives such as #define and #include are handled by the preprocessor, a piece of software that edits C programs just prior to compilation. Its reliance on a preprocessor makes C (along with C++) unique among major programming languages. The preprocessor is a powerful tool, but it also can be a source of hard-to-find bugs. Copyright © 2008 W. W. Norton & Company. All rights reserved.

3 How the Preprocessor Works
The preprocessor looks for preprocessing directives, which begin with a # character. We’ve encountered the #define and #include directives before. #define defines a macro—a name that represents something else, such as a constant. The preprocessor responds to a #define directive by storing the name of the macro along with its definition. When the macro is used later, the preprocessor “expands” the macro, replacing it by its defined value. Copyright © 2008 W. W. Norton & Company. All rights reserved.

4 How the Preprocessor Works
The preprocessor’s role in the compilation process:  Copyright © 2008 W. W. Norton & Company. All rights reserved.

5 Preprocessing Directives
Several rules apply to all directives. Directives always begin with the # symbol. The # symbol need not be at the beginning of a line, as long as only white space precedes it. Any number of spaces and horizontal tab characters may separate the tokens in a directive. Example: # define N Copyright © 2008 W. W. Norton & Company. All rights reserved.

6 Preprocessing Directives
Directives always end at the first new-line character, unless explicitly continued. To continue a directive to the next line, end the current line with a \ character: #define DISK_CAPACITY (SIDES * \ TRACKS_PER_SIDE * \ SECTORS_PER_TRACK * \ BYTES_PER_SECTOR) Copyright © 2008 W. W. Norton & Company. All rights reserved.

7 Preprocessing Directives
Directives can appear anywhere in a program. Although #define and #include directives usually appear at the beginning of a file, other directives are more likely to show up later. Comments may appear on the same line as a directive. It’s good practice to put a comment at the end of a macro definition: #define FREEZING_PT 32.0f /* freezing point of water */ Copyright © 2008 W. W. Norton & Company. All rights reserved.

8 Simple Macros Renaming types An example from Chapter 5:
#define BOOL int Type definitions are a better alternative. Controlling conditional compilation Macros play an important role in controlling conditional compilation. A macro that might indicate “debugging mode”: #define DEBUG Copyright © 2008 W. W. Norton & Company. All rights reserved.

9 Parameterized Macros Definition of a parameterized macro (also known as a function-like macro): #define identifier( x1 , x2 , … , xn ) replacement-list x1, x2, …, xn are identifiers (the macro’s parameters). The parameters may appear as many times as desired in the replacement list. There must be no space between the macro name and the left parenthesis. If space is left, the preprocessor will treat (x1, x2, …, xn) as part of the replacement list. Copyright © 2008 W. W. Norton & Company. All rights reserved.

10 Parameterized Macros Examples of parameterized macros:
#define MAX(x,y) ((x)>(y)?(x):(y)) #define IS_EVEN(n) ((n)%2==0) Invocations of these macros: i = MAX(j+k, m-n); if (IS_EVEN(i)) i++; The same lines after macro replacement: i = ((j+k)>(m-n)?(j+k):(m-n)); if (((i)%2==0)) i++;  Copyright © 2008 W. W. Norton & Company. All rights reserved.

11 Opening a File Opening a file for use as a stream requires a call of the fopen function. Prototype for fopen: FILE *fopen(const char * restrict filename, const char * restrict mode); filename is the name of the file to be opened. This argument may include information about the file’s location, such as a drive specifier or path. mode is a “mode string” that specifies what operations we intend to perform on the file. Copyright © 2008 W. W. Norton & Company. All rights reserved.

12 Structure Variables The properties of a structure are different from those of an array. The elements of a structure (its members) aren’t required to have the same type. The members of a structure have names; to select a particular member, we specify its name, not its position. In some languages, structures are called records, and members are known as fields. Copyright © 2008 W. W. Norton & Company. All rights reserved.

13 Declaring Structure Variables
A structure is a logical choice for storing a collection of related data items. A declaration of two structure variables that store information about parts in a warehouse: struct { int number; char name[NAME_LEN+1]; int on_hand; } part1, part2; Copyright © 2008 W. W. Norton & Company. All rights reserved.

14 Operations on Structures
To access a member within a structure, we write the name of the structure first, then a period, then the name of the member. Statements that display the values of part1’s members: printf("Part number: %d\n", part1.number); printf("Part name: %s\n", part1.name); printf("Quantity on hand: %d\n", part1.on_hand); Copyright © 2008 W. W. Norton & Company. All rights reserved.

15 Pointer to a structure Copyright © 2008 W. W. Norton & Company.
All rights reserved.

16 Opening a File In Windows, be careful when the file name in a call of fopen includes the \ character. The call fopen("c:\project\test1.dat", "r") will fail, because \t is treated as a character escape. One way to avoid the problem is to use \\ instead of \: fopen("c:\\project\\test1.dat", "r") An alternative is to use the / character instead of \: fopen("c:/project/test1.dat", "r") Copyright © 2008 W. W. Norton & Company. All rights reserved.

17 Opening a File fopen returns a file pointer that the program can (and usually will) save in a variable: fp = fopen("in.dat", "r"); /* opens in.dat for reading */ When it can’t open a file, fopen returns a null pointer. Copyright © 2008 W. W. Norton & Company. All rights reserved.

18 Modes Factors that determine which mode string to pass to fopen:
Which operations are to be performed on the file Whether the file contains text or binary data Copyright © 2008 W. W. Norton & Company. All rights reserved.

19 Modes Mode strings for text files: String Meaning "r" Open for reading
"w" Open for writing (file need not exist) "a" Open for appending (file need not exist) "r+" Open for reading and writing, starting at beginning "w+" Open for reading and writing (truncate if file exists) "a+" Open for reading and writing (append if file exists) Copyright © 2008 W. W. Norton & Company. All rights reserved.

20 Modes Mode strings for binary files: String Meaning
"rb" Open for reading "wb" Open for writing (file need not exist) "ab" Open for appending (file need not exist) "r+b" or "rb+" Open for reading and writing, starting at beginning "w+b" or "wb+" Open for reading and writing (truncate if file exists) "a+b" or "ab+" Open for reading and writing (append if file exists) Copyright © 2008 W. W. Norton & Company. All rights reserved.

21 Obtaining File Names from the Command Line
Chapter 13 showed how to access command-line arguments by defining main as a function with two parameters: int main(int argc, char *argv[]) { } argc is the number of command-line arguments. argv is an array of pointers to the argument strings. Copyright © 2008 W. W. Norton & Company. All rights reserved.

22 Obtaining File Names from the Command Line
argv[0] points to the program name, argv[1] through argv[argc-1] point to the remaining arguments, and argv[argc] is a null pointer. In the demo example, argc is 3 and argv has the following appearance: Copyright © 2008 W. W. Norton & Company. All rights reserved.

23 Sample questions Q1. The mode “r+” of opening file in fopen() stands for A. Only reading; B. Reading and writing C. Reading and appending D. None of the above Copyright © 2008 W. W. Norton & Company. All rights reserved.

24 Sample questions Q2. The function fprintf() with the what kind of stream is equivalent to printf()? A. stdin B. stdout C. stderr D. file stream Copyright © 2008 W. W. Norton & Company. All rights reserved.

25 Sample questions Q3. What is the value of argc for the following call?
Demo input.txt output.txt A. 2 B. 3 C. 4 D. void Copyright © 2008 W. W. Norton & Company. All rights reserved.

26 Sample questions Q4.The call is the same as in Q3, what is the value of argv[1]? A. “Demo” B. “input.txt” C. “output.txt” D. Null Copyright © 2008 W. W. Norton & Company. All rights reserved.

27 Sample questions Q6. True or False: Macros cannot have parameters
A. True B. False Copyright © 2008 W. W. Norton & Company. All rights reserved.

28 Sample questions Q7. True or False: Preprocessor statements such as #include and #define can only appear in the beginning of a file. A. True B. False Copyright © 2008 W. W. Norton & Company. All rights reserved.


Download ppt "Exam 4 review Copyright © 2008 W. W. Norton & Company."

Similar presentations


Ads by Google