|
|
|
Chapter 14Answers to Selected Exercises
2. [was #2] 4. [was #4] (a) One problem stems from the lack of parentheses around the replacement list. For example, the statement a = 1/AVG(b, c); will be replaced by a = 1/(b+c)/2;
Even if we add the missing parentheses, though, the macro still has problems, because it needs parentheses around
a = AVG(b<c, c>d); into a = ((b<c+c>d)/2); which is equivalent to a = ((b<(c+c)>d)/2); Here's the final (corrected) version of the macro: #define AVG(x,y) (((x)+(y))/2) (b) The problem is the lack of parentheses around the replacement list. For example, a = 1/AREA(b, c); becomes a = 1/(b)*(c); Here's the corrected macro: #define AREA(x,y) ((x)*(y)) 5. [was #6]
(a) The call of
putchar(('a'<=(s[++i])&&(s[++i])<='z'?(s[++i])-'a'+'A':(s[++i]))); The character
(b) The character 7. [was #8] (a) long long_max(long x, long y) { return x > y ? x : y; } The preprocessor would actually put all the tokens on one line, but this version is more readable.
(b) The problem with types such as unsigned long unsigned long_max(unsigned long x, unsigned long y) { return x > y ? x : y; }
(c) To make typedef unsigned long ULONG;
We can now write
12. [was #10] (c) and (e) will fail, since 14. [was #12; modified] Here's what the program will look like after preprocessing: Blank line Blank line Blank line Blank line Blank line Blank line Blank line int main(void) { int a[= 10], i, j, k, m; Blank line i = j; Blank line Blank line Blank line i = 10 * j+1; i = (x,y) x-y(j, k); i = ((((j)*(j)))*(((j)*(j)))); i = (((j)*(j))*(j)); i = jk; puts("i" "j"); Blank line i = SQR(j); Blank line i = (j); return 0; } Some preprocessors delete white-space characters at the beginning of a line, so your results may vary. Three lines will cause errors when the program is compiled. Two contain syntax errors: int a[= 10], i, j, k, m; i = (x,y) x-y(j, k); The third refers to an undefined variable: i = jk; Copyright © 2008, 1996 W. W. Norton & Company, Inc. All rights reserved. |