"The C Programming Language", 2nd edition,
Kernighan and Ritchie
整理:Xiao Guan(cnguan@gmail.com,
msn:ghpcn@hotmail.com,qq:237811553)
本答案为经典的 c 圣经《c 程序设计语言》英文的配套答案,所列页
码均为英文版的。本想整理为 pdf 格式,考虑到程序能直接运行,最
终还是决定为网页格式,希望能给大家的学习带来帮助!感谢给出答
案的程序工程师,恕不列出他们名字
1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.10 1.11 1.12 1.13 1.14
1.15 1.16 1.17 1.18 1.19 1.20 1.21 1.22 1.23 1.24 2.01 2.02 2.03 2.04
2.05 2.06 2.07 2.08 2.09 2.10 3.01 3.02 3.03 3.04 3.05 3.06 4.01 4.02
4.03 4.04 4.05 4.06 4.07 4.08 4.12 4.13 4.14 5.01 5.02 5.03 5.04 5.05
5.06 5.07 5.08 5.09 5.10 5.11 5.13 5.14 6.01 6.03 6.04 6.05 7.01 7.02
7.03 7.06 7.08 7.09 8.01 8.03 8.04 8.06
Answer to Exercise 1-1
Run the "hello, world" program on your system. Experiment with leaving out parts of
the program, to see what error messages you get.
Murphy's Law dictates that there is no single correct answer to the very first exercise
in the book. Oh well. Here's a "hello world" program:
#include
int main(void)
{
printf("hello, world\n");
return 0;
}
As you can see, I've added a return statement, because main always returns int, and
it's good style to show this explicitly.
Answer to Exercise 1-2
Experiment to find out what happens when printf 's argument string contains \c,
where c is some character not listed above.
By 'above', the question is referring to:
\n (newline)
\t (tab)
\b (backspace)
\" (double quote)
\\ (backslash) We have to tread carefully here, because using a non-specified escape
sequence invokes undefined behaviour. The following program attempts to
demonstrate all the legal escape sequences, not including the ones already shown
(except \n , which I actually need in the program), and not including hexadecimal and
octal escape sequences.
#include
int main(void)
{
printf("Audible or visual alert. \a\n");
printf("Form feed. \f\n");
printf("This escape, \r, moves the active position to the initial
position of the current line.\n");
printf("Vertical tab \v is tricky, as its behaviour is un specified under
certain conditions.\n");
return 0;
}
Answer to Exercise 1-3
Modify the temperature conversion program to print a heading above the table.
#include
int main(void)
{
float fahr, celsius;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;
printf("F C\n\n");
fahr = lower;
while(fahr <= upper)
{
celsius = (5.0 / 9.0) * (fahr - 32.0);
printf("%3.0f %6.1f\n", fahr, celsius);
fahr = fahr + step;
}
return 0;
}
Answer to Exercise 1-4
Write a program to print the corresponding Celsius to Fahrenheit table.
#include
int main(void)
{
float fahr, celsius;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;
printf("C F\n\n");
celsius = lower;
while(celsius <= upper)
{
fahr = (9.0/5.0) * celsius + 32.0;
printf("%3.0f %6.1f\n", celsius, fahr);
celsius = celsius + step;
}
return 0;
}
Answer to Exercise 1-5
Modify the temperature conversion program to print the table in reverse order, that is,
from 300 degrees to 0.
This version uses a while loop:
#include
int main(void)
{
float fahr, celsius;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;
printf("C F\n\n");
celsius = upper;
while(celsius >= lower)
{
fahr = (9.0/5.0) * celsius + 32.0;
printf("%3.0f %6.1f\n", celsius, fahr);
celsius = celsius - step;
}
return 0;
}
This version uses a for loop:
#include
int main(void)
{
float fahr, celsius;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;
printf("C F\n\n");
for(celsius = upper; celsius >= lower; celsius = celsius - step)
{
fahr = (9.0/5.0) * celsius + 32.0;
printf("%3.0f %6.1f\n", celsius, fahr);
}
return 0;
}
Chris Sidi notes that Section 1.3 Has a short For statement example, and "Based on
that example, I think the solution to 1.5:
a) should do fahr to celsius conversion (whereas the solutions on your page do celsius
to fahr)
b) should be similar to the example and as small." He offers this solution:
#include
/* print Fahrenheit-Celsius table */
int
main()
{
int fahr;
for (fahr = 300; fahr >= 0; fahr = fahr - 20)
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
return 0;
}
Answer to Exercise 1-6
Verify that the expression getchar() != EOF is 0 or 1.
/* This program prompts for input, and then captures a character
* from the keyboard. If EOF is signalled (typically through a
* control-D or control-Z character, though not necessarily),
* the program prints 0. Otherwise, it prints 1.
*
* If your input stream is buffered (and it probably is), then
* you will need to press the ENTER key before the program will
* respond.
*/
#include
int main(void)
{
printf("Press a key. ENTER would be nice :-)\n\n");
printf("The expression getchar() != EOF evaluates to %d \n", getchar() !=
EOF);
return 0;
}
Answer to Exercise 1-7
Write a program to print the value of EOF .
#include
int main(void)
{
printf("The value of EOF is %d\n\n", EOF);
return 0;
}
Exercise 1-8
Write a program to count blanks, tabs, and newlines.
#include
int main(void)
{
int blanks, tabs, newlines;
int c;
int done = 0;
int lastchar = 0;
blanks = 0;
tabs = 0;
newlines = 0;
while(done == 0)
{
c = getchar();
if(c == ' ')
++blanks;
if(c == '\t')
++tabs;
if(c == '\n')
++newlines;
if(c == EOF)
{
if(lastchar != '\n')
{
++newlines; /* this is a bit of a semantic stretch, but it copes
* with implementations where a text file might not
* end with a newline. Thanks to Jim Stad for pointing
* this out.
*/
}
done = 1;
}
lastchar = c;
}
printf("Blanks: %d\nTabs: %d\nLines: %d\n", blanks, tabs, newlines);
return 0;
}
Exercise 1-9
Write a program to copy its input to its output, replacing each string of one or more
blanks by a single blank.
#include
int main(void)
{
int c;
int inspace;
inspace = 0;
while((c = getchar()) != EOF)
{
if(c == ' ')
{
if(inspace == 0)
{
inspace = 1;
putchar(c);
}
}
/* We haven't met 'else' yet, so we have to be a little clumsy */
if(c != ' ')
{
inspace = 0;
putchar(c);
}
}
return 0;
}
Chris Sidi writes: "instead of having an "inspace" boolean, you can keep track of the
previous character and see if both the current character and previous character are
spaces:"
#include
/* count lines in input */
int
main()
{
int c, pc; /* c = character, pc = previous character */
/* set pc to a value that wouldn't match any character, in case