天天看点

(C语言之复习demo_03--自我复习使用-可供参考)宏常量, const常量 及 printf 和scanf语句 对 %f 和 %lf 使用注意

#include <stdio.h>
#define MY_PI 3.14159
/*
	(1)init part - to give a init or a common part which is necessary for program that can't
	be given by lines of commentary.

	(2)indepent part - only one part can be existed in all demo when executing a program,
	Meanwhile,Any other "independent part" shoud be given  commentary lines on it.

*/

int main()
{
	/*
	--independent block1
	编程计算并输出半径r = 5.3 的圆的周长和面积
*/
	//double r = 5.3;
	//double area = 0;
	//double circumference = 0;

	//circumference = 2 * 3.14159 * r;//[səˈkʌmfərəns] circumference  圆周长
	//area = 3.14159 * r * r;
	//printf("c = %f\n", circumference); 
	//printf("s = %f\n", area);



/*
	--independent block2
	编程从键盘输入圆的半径r, 计算并输出圆的周长和面积
*/
	//double radius = 0;// radius [ˈreɪdiəs] 半径(距离); 用半径度量的圆形面积; 半径范围; 桡骨; radii ['reɪdɪaɪ]
	//double circumference = 0;
	//double area = 0;

	//printf("please input radius:");
	//scanf_s("%lf", &radius);
	//circumference = 2 * 3.14159 * radius;
	//area = 3.14159 * radius * radius;
	//printf("circumference = %f\narea = %f\n", circumference, area);


	/*
		--independent block3
		使用宏常量定义PI, 编程从键盘输入圆的半径r, 计算并输出圆的周长和面积
	*/
	/*double radius = 0;

	printf("please input the radius:");
	scanf_s("%lf", &radius);
	printf("circumference = %f\narea = %f\n", 2 * MY_PI * radius, MY_PI * radius * radius);*/


	/*
		--independent block4
		使用const常量定义PI,编程从键盘输入圆的半径,计算并输出圆的周长和面积
	*/
	//const double PI = 3.14159;
	//double radius;

	//printf("please input the radius:");
	//scanf_s("%lf", &radius);
	//printf("circumference = %f\narea = %lf\n", 2 * PI * radius, PI * radius * radius);

	/*
	CONCLUSION:

	宏定义:
	是一种编译预处理命令,不是C语句, 不能加分号作为语句
	在编译预处理阶段,程序将宏定义之后所以出现的标识符PI均用3.14159所替代, 将宏名替换为字符串的过程称为宏替换

	宏常量:
	宏常量也称符号常量,一般采用全大写字母表示
	宏常量没有数据类型,编译器不对宏常量进行类型检查,不检查,出错的话就不易发觉.

	CONST常量:
	声明时, 将const类型修饰符放在类型名之前.
	编译器将const常量放在只读存储区, 不允许在程序中改变其值,因此const常量只能在定义时赋初值.


	Attentions:
	printf语句中输出控制符对float 和 double 类型不进行严格区分, 可以都使用%f
	scanf语句, 输入控制符对float 和 double 类型 进行严格区分,double 必须使用%lf 格式输入

	Thoughts:
	定义变量的意义,不仅仅是为了保存一个数, 更多是为了接收一个结果数,
	或者,变量中保存的数在程序运行过程中经常需要变化
	如果已知的一个数 完全不会作改变,, 可以定义为宏常量或者const常量

*/

	return 0;
}
           

继续阅读