代码
2.1 pun.c
#include <stdio.h>int main(void){printf("To C, not to C: that is the question\n");return 0;}
这里仅做简要介绍。程序中第一行
#include <stdio.h>
是必不可少的,它“包含”了C语言标准输入/输出库的相关信息。程序的可执行代码都在main 函数中,这个函数代表“主”程序。main函数中的第一行代码是用来显示期望信息的。printf 函数来自标准输入/输出库,可以产生完美的格式化输出。代码\n 告诉printf 函数执行完消息显示后要进行换行操作。第二行代码,
return 0;
表明程序终止时会向操作系统返回值0。
2.4 dweight.c
程序 计算箱子的空间重量
运输公司特别不喜欢又大又轻的箱子,因为箱子在卡车或飞机上运输时要占据宝贵的空间。事实上,对于这类箱子,公司常常要求按照箱子的体积而不是重量来支付额外的费用。在美国,通常的做法是把体积除以166(这是每磅允许的立方英寸数)。如果除得的商(也就是箱子的“空间”重量或“体积”重量)大于箱子的实际重量,那么运费就按照空间重量来计算。(除数166是针对国际运输的,计算国内运输的空间重量时通常用194代替。)假设运输公司雇你来编写一个计算箱子空间重量的程序。因为刚刚开始学习C语言,所以你决定先编写一个计算特定箱子空间重量的程序来试试身手,其中箱子的长、宽、高分别是12英寸、10英寸和8英寸。C语言中除法运算用符号/ 表示。所以,很显然计算箱子空间重量的公式如下:
weight = volume / 166;
这里的weight 和volume 都是整型变量,分别用来表示箱子的重量和体积。但是上面这个公式并不是我们所需要的。在C语言中,如果两个整数相除,那么结果会被“截短”:小数点后的所有数字都会丢失。12英寸×10英寸×8英寸的箱子体积是960立方英寸,960除以166的结果是5而不是5.783,这样使得重量向下取整;而运输公司则希望结果向上取整。一种解决方案是在除以166之前把体积数加上165:
weight = (volume + 165) / 166;
这样,体积为166的箱子重量就为331/166,取整为1;而体积为167的箱子重量则为332/166,取整为2。下面给出了利用这种方法编写的计算空间重量的程序。
/* Computes the dimensional weight of a 12" x 10" x 8" box */#include <stdio.h>int main(void){int height, width, length, volume, weight;height = 8;length = 12;width = 10;volume = height * width * length;weight = (volume + 165) / 166;printf("Demension: %dx%dx%d\n", length, width, height);printf("Volume (cubic inches) : %d\n", volume);printf("Dimonsional weight (pounds) : %d\n", weight);return 0;}
2.5 dweight2.c
程序 计算箱子的空间重量(改进版)
下面是计算空间重量程序的一个改进版。在这个改进的程序中,用户可以录入尺寸。注意,每一个scanf 函数调用都紧跟在一个printf函数调用的后面。这样做可以提示用户何时输入,以及输入什么。
/* Computes the dimensional weight of a box from input provided by the user */#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>int main(void){int height, width, length, volume, weight;printf("Enter height of box: ");scanf("%d", &height);printf("Enter width of box: ");scanf("%d", &width);printf("Enter length of box: ");scanf("%d", &length);volume = height * width * length;weight = (volume + 165) / 166;printf("Volume (cubic inches) : %d\n", volume);printf("Dimonsional weight (pounds) : %d\n", weight);return 0;}
提示用户输入的消息(提示符)通常不应该以换行符结束,因为我们希望用户在同一行输入。这样,当用户敲回车键时,光标会自动移动到下一行,因此就不需要程序通过显示换行符来终止当前行了。dweight2.c 程序还存在一个问题:如果用户输入的不是数值,程序就会出问题。3.2节会更详细地讨论这个问题。
2.6 celsius.c
程序 华氏温度转换为摄氏温度
下面的程序提示用户输入一个华氏温度,然后输出一个对应的摄氏温度。此程序的输出格式如下(跟前面的例子一样,用户的输入信息用下划线标注出来):
Enter Fahrenheit temperature: 212
Celsius equivalent: 100.0
这段程序允许温度值不是整数,这也是摄氏温度显示为100.0 而不是100 的原因。首先来阅读一下整个程序,随后再讨论程序是如何构成的。
/* Converts a Fahrenheit temperature to Celsius*/#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#define FREEZING_PT 32.0f#define SCALE_FACTOR (5.0f / 9.0f)int main(void){float fahrenheit, celsius;printf("Enter Fahrenheit temperature: ");scanf("%f", &fahrenheit);celsius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR;printf("Celsius equivalent: %.1f\n", celsius);return 0;}
练习题
1.建立并运行由Kernighan和Ritchie编写的著名的“hello,world”程序:
#include <stdio.h>int main (void){printf("hello, world\n");}
在编译时是否有警告信息?如果有,需要如何进行修改呢?
Solution
Compiling the program produced no errors using gcc version 7.2.0. Here is the complete output of run commands:
$ gcc 1.c -o 1 -Wall -W -pedantic -std=c99(no output, no warnings or errors)$ gcc 1.c -o 1 -Wall -W -pedantic -std=c891.c: in function main:1.c:5:1: warning: control reaches end of non-void function [-Wreturn-type]}^
This warning can be fixed by amendingreturn 0;
to the program (see1b.c
):
$ gcc 1b.c -o 1b -Wall