1. 程式人生 > >k&r課後習題整理——第一章

k&r課後習題整理——第一章

最近閒的有點無聊,打算重新夯實一下C語言基礎,順便做一做K&R的課後習題
1-1、自己試試
1-2、printf(“\c”)的時候,只剩一個c了,那麼這是為什麼呢?
c語言前定義了一些+字母表示平常不能顯示的ASCII字元,除了特定的幾個字母以外,其他的就只能表示字母本身啦
1-3、程式開頭增加一行printf();即可
1-4、很簡單,找到攝氏如何轉華氏公式轉換一下就行

#include "stdio.h"
main()
{
    float fahr, celsius;
    float lower, upper, step;
    printf("Temple Change List\n"
); printf("\n"); lower = 0; /* lower limit of temperatuire scale */ upper = 300; /* upper limit */ step = 20; /* step size */ celsius = lower; while (celsius <= upper) { fahr = celsius * 1.8 + 32; printf("%3.0f %6.1f\n",celsius, fahr); celsius = celsius + step; } }

這是輸出結果

1-5、輸出程式碼如下
#include "stdio.h"
main()
{
float fahr, celsius;
float lower, upper, step;
printf("Temple Change List\n");
printf("\n");
lower = 0; /* lower limit of temperatuire scale */
upper = 300; /* upper limit */
step = 20; /* step size */
celsius = upper;
while (celsius >= lower) {
fahr = celsius * 1.8 + 32;
printf("%3.0f %6.1f\n",celsius, fahr);
celsius -= step;
}
}