C程序设计语言 (第二版) 课后答案第一章

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Chapter 1

Exercise 1-1

Run the “hello world” program on your system. Experiment with leaving out parts of the program, to see what error message you get.

#include

int main()

{

printf("hello, ");

printf("world");

printf("\n");

return 0;

}

Exercise 1-2

Experiment to find out what happens when printf’s argument string contains \c, where c is some character not list above.

Exercise 1-3

Modify the temperature conversion program to print a heading above the table.

#include

int main()

{

float fahr, celsius;

float lower, upper, step;

lower = 0;

upper = 300;

step = 20;

fahr = lower;

printf("Fahrenheit temperatures and their centigrade or Celsius equivalents\n");

while (fahr <= upper)

{

celsius = (5.0/9.0) * (fahr-32.0);

printf("%3.0f %6.1f\n", fahr, celsius);

fahr = fahr + step;

}

return 0;

}

Exercise 1-4

Write a program to print the corresponding Celsius to Fahrenheit table.

#include

int main()

{

float fahr, celsius;

float lower, upper, step;

lower = 0;

upper = 300;

step = 20;

celsius = lower;

while (celsius<= upper)

{

fahr = 9.0*celsius/5.0+32;

printf("%3.0f %6.1f\n", celsius,fahr);

celsius = celsius + step;

}

return 0;

}

Exercise 1-5

Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.

#include

int main()

{

float fahr, celsius;

float lower, upper, step;

lower = 0;

upper = 300;

step = 20;

fahr = upper;

printf("Fahrenheit temperatures and their centigrade or Celsius equivalents\n");

while (fahr >= lower)

{

celsius = (5.0/9.0) * (fahr-32.0);

printf("%3.0f %6.1f\n", fahr, celsius);

fahr = fahr - step;

}

return 0;

}

Exercise 1-6

Verify that the expression getchar()!=EOF is 0 or 1.

#include

main()

{

int c;

c =(getchar() != EOF);

printf("%d",c);

return 0;

}

Exercise 1-7

Write a program to print the value of EOF .

#include

int main()

{

printf("%d",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;

printf("输入0查看结果\n");

while(done == 0)

{

c = getchar();

if(c == ' ')

++blanks;

if(c == '\t')

++tabs;

if(c == '\n')

++newlines;

if(c == '0')

{

if(lastchar != '\n')

相关文档
最新文档