本章思维导图
本章代码
page42_程序清单_2_1_first.c
#include <stdio.h>
int main(void) /* 一个简单的 C 程序 */
{
int num; /* 定义一个名为 num 的变量 */
num = 1; /* 为 num 赋一个值 */
printf("I am a simple "); /* 使用 printf()函数 */
printf("computer.\n");
printf("My favorite number is %d because it is first.\n", num);
return 0;
}
运行结果:
page52_程序清单_2_2_fathm_ft.c
// fathm_ft.c -- 把 2 音寻转换成英寸
#include <stdio.h>
int main(void)
{
int feet, fathoms;
fathoms = 2;
feet = 6 * fathoms;
printf("There are %d feet in %d fathoms!\n", feet, fathoms);
printf("Yes, I said %d feet!\n", 6 * fathoms);
return 0;
}
运行结果:
page54_程序清单_2_3_two_func.c
//* two_func.c -- 一个文件中包含两个函数 */
#include <stdio.h>
void butler(void); /* ANSI/ISO C 函数原型 */
int main(void)
{
printf("I will summon the butler function.\n");
butler();
printf("Yes. Bring me some tea and writeable DVDs.\n");
return 0;
}
void butler(void) /* 函数定义开始 */
{
printf("You rang, sir?\n");
}
运行结果:
page55_程序清单_2_4_nogood.c
/* nogood.c -- 有错误的程序 */
#include <stdio.h>
int main(void)
(
int n, int n2, int n3;
/* 该程序有多处错误
n = 5;
n2 = n * n;
n3 = n2 * n2;
printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3)
return 0;
)
运行结果: