Escolar Documentos
Profissional Documentos
Cultura Documentos
Preprocessor
Hold thou the good; dene it well.
Alfred, Lord Tennyson
OBJECTIVES
In this chapter you will learn:
Conditions are
fundamentally sound.
Herbert Hoover [December
1929]
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Outline
Chapter F Preprocessor
F.1
F.2
F.3
F.4
F.5
F.6
F.7
F.8
F.9
F.10
Introduction
The #include Preprocessor Directive
The #define Preprocessor Directive: Symbolic Constants
The #define Preprocessor Directive: Macros
Conditional Compilation
The #error and #pragma Preprocessor Directives
The # and ## Operators
Predefined Symbolic Constants
Assertions
Wrap-Up
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Instructors Manual
Instructors Manual
F.1
e) The
ANS: #undef.
f) The
defined(name)
g)
!defined(name).
h) The
macro prints a message and terminates program execution if the value
of the expression the macro evaluates is 0.
ANS: assert.
i) The
j) The
k) The
ANS: #include.
ANS: ##.
ANS: #.
l) The character
indicates that the replacement text for a symbolic constant
or macro continues on the next line.
ANS: \.
F.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// exF_02.cpp
// Self-review exercise F.2 solution.
#include <iostream>
using std::cout;
using std::endl;
int main()
{
cout <<
<<
<<
<<
<<
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
__LINE__,
4
16
17
18
Appendix F Preprocessor
return 0;
}
// end main
__LINE__ = 10
__FILE__ = c:\cpphtp5_examples\appF\exF_02.cpp
__DATE__ = Jul 17 2002
__TIME__ = 09:55:58
__cplusplus = 199711
F.3
c) Include the header file common.h. The header is found in the same directory as the file
being compiled.
ANS: #include "common.h"
d) If symbolic constant
#ifdef.
TRUE
e) If symbolic constant TRUE is defined, undefine it, and redefine it as 1. Use the #ifdef
preprocessor directive.
ANS: #ifdef TRUE
#undef TRUE
#define TRUE 1
#endif
g) Define macro CUBE_VOLUME that computes the volume of a cube (takes one argument).
ANS: #define CUBE_VOLUME( x )
( ( x ) * ( x ) * ( x ) )
Solutions
F.4
Write a program that defines a macro with one argument to compute the volume of a
sphere. The program should compute the volume for spheres of radii from 1 to 10 and print the
results in tabular format. The formula for the volume of a sphere is
( 4.0 / 3 ) * * r3
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Solutions
where is 3.14159.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
F.5
Volume
4.189
33.510
113.097
268.082
523.598
904.778
1436.754
2144.659
3053.625
4188.787
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Appendix F Preprocessor
The program should define macro SUM with two arguments, x and y, and use SUM to produce the output.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
F.6
Write a program that uses macro MINIMUM2 to determine the smaller of two numeric values.
Input the values from the keyboard.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Solutions
28
29
30
31
32
33
34
35
36
37
38
39
40
cout << "The minimum of " << a << " and " << b << " is "
<< MINIMUM2( a, b ) << "\n\n";
// ask user for and store two double values
cout << "Enter two doubles: ";
cin >> c >> d;
// use macro MINIMUM to determine and display smallest double entered
cout << fixed << showpoint;
cout << "The minimum of " << setprecision( 2 ) << c << " and " << d
<< " is " << MINIMUM2( c, d ) << '\n';
return 0;
} // end main
F.7
Write a program that uses macro MINIMUM3 to determine the smallest of three numeric values. Macro MINIMUM3 should use macro MINIMUM2 defined in Exercise F.6 to determine the smallest
number. Input the values from the keyboard.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
8
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Appendix F Preprocessor
double f;
// ask user for and store three integer values
cout << "Enter three integers: ";
cin >> a >> b >> c;
// use macro MINIMUM3 to determine smallest of three integers input
cout << "The minimum of " << a << ", " << b << ", and " << c
<< " is " << MINIMUM3( a, b, c ) << "\n\nEnter three doubles: ";
// store user input doubles
cin >> d >> e >> f;
// use macro MINIMUM3 to determine smallest of three doubles input
cout << fixed << showpoint;
cout << "The minimum of " << setprecision( 2 ) << d << ", "
<< e << ", and " << f << " is " << MINIMUM3( d, e, f ) << '\n';
return 0;
} // end main
F.8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Solutions
F.9
Write a program that uses macro PRINTARRAY to print an array of integers. The macro
should receive the array and the number of elements in the array as arguments.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
F.10 Write a program that uses macro SUMARRAY to sum the values in a numeric array. The macro
should receive the array and the number of elements in the array as arguments.
ANS:
1
2
3
4
5
6
7
8
9
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
10
10
11
12
13
14
15
16
17
18
19
20
21
22
Appendix F Preprocessor
Sum is 55
F.11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Solutions
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
11
12
Appendix F Preprocessor
88
cout << "\nFunction minimum2 as an inline function:\n"
89
<< "The minimum of " << x << " and " << y << " is "
90
<< minimum2( x, y ) << '\n';
91
92
int z = 4;
93
94
// determine minimum of x, y and z using inline function minimum3
95
cout << "\nFunction minimum3 as an inline function:\n"
96
<< "The minimum of " << x << ", " << y << " and " << z << " is "
97
<< minimum3( x, y, z ) << '\n';
98
99
// display string s using inline function print
100
char s[] = "string..."; // string to be printed
101
cout << "\nFunction print as an inline function:\n"
102
<< "The output of print is: ";
103
print( s );
104
cout << endl;
105
106
// array to display and add up elements
107
int b[ SIZE ] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
108
cout << "\nThe array contains elements ";
109
printArray( b, SIZE ); // display elements of array
110
cout << endl;
111
cout << "\nThe sum of the elements in the array is " // sum elements
112
<< sumArray( b, SIZE ) << endl;
113
return 0;
114 } // end main
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Solutions
13
8 10 12 14 16 18 20
F.12 For each of the following macros, identify the possible problems (if any) when the preprocessor expands the macros:
a) #define SQR( x ) x * x
ANS: The macro arguments are not enclosed in parentheses in the replacement text. Errors
b) #define SQR( x ) ( x * x )
ANS: Now the entire replacement text is placed in parentheses, but the individual argu-
ments are not. The same problem can occur as in the previous part.
c) #define SQR( x ) ( x ) * ( x )
ANS: When SQR( x ) is used in an expression
d) #define SQR( x ) ( ( x ) * ( x ) )
ANS: No problems.
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.