C 程序设计语言

C 程序设计语言

导言

1.1 入门

#include <stdio.h>
#include <stdlib.h>

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

第一个程序,按照惯例,来个helloworld~

java语言是以“.java”作为文件的扩展名.

在c语言中,以“.c”作为文件的扩展名.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
	printf("hello world
			");
}

在printf函数的参数中,只能用\n表示换行符.如果用程序的换行代替\n,如上所示,c编译器会产生一条错误信息.

printf函数永远不会自动换行.

#include <stdio.h>
#include <stdlib.h>

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

这段代码与前面的程序的输出相同.

请注意, \n只代表一个字符. 类似于\n的转义字符序列为表示无法输入的字符或不可见字符提供了一种通用的可扩充机制.

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

#include <stdio.h>
#include <stdlib.h>

int main(void) {
	printf("hello, world");
}
// 省略了换行符(\n),这将使光标停留在输出信息的末尾
#include <stdio.h>
#include <stdlib.h>

int main(void) {
	printf("hello, world")
}
// 省略printf()后面的分号.C程序的语句必须以分号结尾.本例中,编译器会报错
#include <stdio.h>
#include <stdlib.h>

int main(void) {
	printf("hello, world\n');
}
// ..\src\CTest.c:15:9: error: missing terminating " character
// 本例中,\n后面的双引号"被错写成单引号'.于是,这个单引号及其后面的右括号和分号被看作整个输出字符串的一部分,编译器报错如上.

练习1-2 做个实验,当printf函数的参数字符串中包含\c时, 观察一下会出现的情况.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
	printf("hello, world\c");
	printf("hello, world\y");
	printf("hello, world\7");
	printf("hello, world\?");
}
// 我的编译器显示内容:
//..\src\CTest.c: In function 'main':
//..\src\CTest.c:15:9: warning: unknown escape sequence: '\c'
//  printf("hello, world\c");
//         ^~~~~~~~~~~~~~~~
//..\src\CTest.c:16:9: warning: unknown escape sequence: '\y'
//  printf("hello, world\y");

如果\后面紧跟的字符不再以上指定的字符中,则其行为是未定义的.

上面代码的执行结果与具体的编译器相关.

1.2 变量与算术表达式

#include <stdio.h>

/**
 * 当fahr= 0, 20, ..., 300时,分别打印华氏摄氏度与摄氏温度对照表
 */
int main(void) {
	int fahr, celsius;
	int lower, upper, step;

	lower = 0;		/* 温度表的下限*/
	upper = 300;	/* 温度表的上限*/
	step = 20;		/* 步长*/

	fahr = lower;
	while (fahr <= upper){
		celsius = 5 * (fahr - 32) / 9;
		printf("%d\t%d\n", fahr, celsius);
		fahr = fahr + step;
	}
}
// 控制台打印结果:
0	-17
20	-6
40	4
60	15
80	26
... ...
280	137
300	148

在该语句中,之所以把表达式写成先乘5然后再除以9而不是直接写成5/9, 其原因是在C语言及许多语言中,整数除法操作将执行舍位,结果中的任何小数点都会被舍弃.由于5和9都是整数, 5/9后经截取所得的结果是0, 因此这样求得的所有摄氏温度都为0.

在printf语句中的第一个参数的%d中指明打印宽度,则打印的数字会在打印区右对齐.

...
printf("%3d\t%6d\n", fahr, celsius);
...
// 打印结果:
  0	   -17
 20	    -6
 40	     4
...    ...
240	   115
260	   126
280	   137
300	   148

改进,用浮点算术运算代替整形算术运算。

#include <stdio.h>

/**
 * 当fahr= 0, 20, ..., 300时,分别打印华氏摄氏度与摄氏温度对照表
 */
int main(void) {
	float fahr, celsius;
	int lower, upper, step;

	lower = 0;		/* 温度表的下限*/
	upper = 300;	/* 温度表的上限*/
	step = 20;		/* 步长*/

	fahr = lower;
	while (fahr <= upper){
		celsius = (5.0 / 9.0) * (fahr - 32.0);
		printf("%3.0f\t%6.1f\n", fahr, celsius);
		fahr = fahr + step;
	}
}
// 打印结果:
  0	 -17.8
 20	  -6.7
 40	   4.4
...  ...
280	 137.8
300	 148.9

练习1-3 在转换表顶部打印一个标题

#include <stdio.h>

/**
 * 当fahr= 0, 20, ..., 300时,分别打印华氏摄氏度与摄氏温度对照表
 */
int main(void) {
	float fahr, celsius;
	int lower, upper, step;

	lower = 0;		/* 温度表的下限*/
	upper = 300;	/* 温度表的上限*/
	step = 20;		/* 步长*/

	printf("Fahr  Celsius\n");
	fahr = lower;
	while (fahr <= upper){
		celsius = (5.0 / 9.0) * (fahr - 32.0);
		printf("%3.0f\t%6.1f\n", fahr, celsius);
		fahr = fahr + step;
	}
}
// 打印结果:
Fahr  Celsius
  0	 -17.8
 20	  -6.7
 40	   4.4
...  ...
240	 115.6
260	 126.7
280	 137.8
300	 148.9

练习1-4 摄氏温度转换为华氏温度

#include <stdio.h>

/**
 * 当fahr= 0, 20, ..., 300时,分别打印摄氏温度与华氏摄氏度对照表
 */
int main(void) {
	float fahr, celsius;
	int lower, upper, step;

	lower = 0;		/* 温度表的下限*/
	upper = 300;	/* 温度表的上限*/
	step = 20;		/* 步长*/

	printf("Celsius  Fahr\n");
	celsius = lower;
	while (celsius <= upper){
		fahr = (celsius * 9.0) / 5.0 + 32.0;
		printf("%3.0f\t%6.1f\n",celsius, fahr);
		celsius = celsius + step;
	}
}
// 打印结果:
Celsius  Fahr
  0	  32.0
 20	  68.0
 40	 104.0
...  ...
240	 464.0
260	 500.0
280	 536.0
300	 572.0
posted @ 2020-08-11 23:26  accordionmaster  阅读(840)  评论(0)    收藏  举报