Chapter 2
Answers to Selected Exercises
2. [was #2] (a) The program contains one directive (#include) and four statements (three calls of printf and one return). (b)
Parkinson's Law:
Work expands so as to fill the time available for its completion. 3. [was #4]
#include
int main(void) {
int height = 8, length = 12, width = 10, volume;
volume = height * length * width;
printf(\ printf(\
printf(\
return 0; }
4. [was #6] Here's one possible program: #include
int main(void) {
int i, j, k; float x, y, z;
printf(\ printf(\ printf(\
printf(\ printf(\
printf(\
return 0; }
When compiled using GCC and then executed, this program produced the following output: Value of i: 5618848 Value of j: 0
Value of k: 6844404 Value of x: 3.98979e-34 Value of y: 9.59105e-39 Value of z: 9.59105e-39
The values printed depend on many factors, so the chance that you'll get exactly these numbers is small.
5. [was #10] (a) is not legal because 100_bottles begins with a digit. 8. [was #12] There are 14 tokens: a, =, (, 3, *, q, -, p, *, p, ), /, 3, and ;.
Answers to Selected Programming Projects 4. [was #8; modified]
#include
int main(void) {
float original_amount, amount_with_tax;
printf(\ scanf(\
amount_with_tax = original_amount * 1.05f;
printf(\
return 0; }
The amount_with_tax variable is unnecessary. If we remove it, the program is slightly shorter: #include
int main(void) {
float original_amount;
printf(\ scanf(\
printf(\
return 0; }
Chapter 3
Answers to Selected Exercises 2. [was #2]
(a) printf(\(b) printf(\(c) printf(\(d) printf(\
5. [was #8] The values of x, i, and y will be 12.3, 45, and .6, respectively. Answers to Selected Programming Projects 1. [was #4; modified]
#include
int main(void) {
int month, day, year;
printf(\ scanf(\
printf(\
return 0; }
3. [was #6; modified]
#include
int main(void) {
int prefix, group, publisher, item, check_digit;
printf(\
scanf(\&check_digit);
printf(\
printf(\ printf(\ printf(\
printf(\
/* The five printf calls can be combined as follows:
printf(\code: %d\\nItem number: %d\\nCheck digit: %d\\n\
prefix, group, publisher, item, check_digit); */
return 0; }
Chapter 4
Answers to Selected Exercises
2. [was #2] Not in C89. Suppose that i is 9 and j is 7. The value of (-i)/j could be either –1 or –2, depending on the implementation. On the other hand, the value of -(i/j) is always –1, regardless of the implementation. In C99, on the other hand, the value of (-i)/j must be equal to the value of -(i/j). 9. [was #6] (a) 63 8 (b) 3 2 1 (c) 2 -1 3 (d) 0 0 0
相关推荐: