<THE C PROGRAMMING LANGUAGE> 练习 第一章 (1-1 -- 1-5)

[b]1-1 在你自己的系统中运行 "hello, world" 程序. 再有意去掉程序中的部分内容, 看看会得到什么错误信息.[/b]



#include <stdio.h>

int
main()
{
printf("hello, world\n");

return 0;
}


输出了 (后面一行是 Code::Blocks 加的, 很不错哈 :wink: ):
[quote]hello, world
Process returned 0 (0x0) execution time : 0.055 s[/quote]

我把
#include <stdio.h>

删除了, 编译通过, 有个警告, 可以运行.


[b]1-2 做个试验, 当 printf 函数的参数字符串中包含 \[i]c[/i] (其中 [i]c[/i] 是上面的转义字符序列中未曾列出的某一字符) 时, 观察一下会出现什么情况.[/b]


编译时出现一个警告, 字符直接输出了, 不包含 "\" .


[b]1-3 修改温度转换程序, 使之能在转换表的顶部打印一个标题.[/b]



#include <stdio.h>

int
main()
{
float fahr, celsius;
int lower, upper, step;

lower = 0;
upper = 300;
step = 20;

printf("F:\tC:\n");

fahr = lower;

while (fahr <= upper) {
celsius = (5.0/9.0) * (fahr - 32.0);
printf("%3.0f\t%6.1f\n", fahr, celsius);

fahr += step;
}

return 0;
}



[b]1-4 编写一个程序打印摄氏度转换为相应华氏温度的转换表.[/b]



#include <stdio.h>

int
main()
{
float celsius, fahr;
int lower, upper, step;

lower = 0;
upper = 30;
step = 2;

printf("C:\tF:\n");

celsius = lower;

while (celsius <= upper) {
fahr = ((9.0/5.0) * celsius) + 32.0;
printf("%6.1f\t%3.0f\n", celsius, fahr);

celsius += step;
}

return 0;
}


输出:
[quote]
C: F:
0.0 32
2.0 36
4.0 39
6.0 43
8.0 46
10.0 50
12.0 54
14.0 57
16.0 61
18.0 64
20.0 68
22.0 72
24.0 75
26.0 79
28.0 82
30.0 86
[/quote]


[b]1-5 修改温度转换程序, 要求以逆序 (即按照从 300 度到 0 度的顺序) 打印温度转换表[/b]



#include <stdio.h>

int
main()
{
float fahr, celsius;
int lower, upper, step;

lower = 0;
upper = 300;
step = 20;

printf("F:\tC:\n");

fahr = upper;

while (fahr > lower) {
celsius = (5.0/9.0) * (fahr - 32.0);
printf("%3.0f\t%6.1f\n", fahr, celsius);

fahr -= step;
}

return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值