(C语言之复习demo_07--自我复习使用-可供参考)屏幕输入输出_格式说明的格式修饰符用法_及scanf函数单项数据输入结束判别标志

#include <stdio.h>

/*
	(1)init part - to give a init or a common part which is necessary for prog-
	-ram and can't be given  commentary lines  added on it.

	(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()
{
	/*
	在输入输出函数中:
		格式说明由%开始,并以格式字符结束,用于指定各输出值参数的输出格式.

	函数printf()中的格式修饰符:
		在函数printf()的格式说明中,可以在%和格式符之间插入格式修饰符,用于对输出格式进
		行微调,如指定输出数据的域宽,显示精度,左对齐,右对齐等.
	*/

	/*
		--independent block1
		使用const常量定义pi,编程从键盘输入圆的半径radius,计算并输出圆的周长和面积
		使其输出的数据保留两位小数点.
	*/
	const double pi = 3.14159;
	double radius, circumference, area;


	printf("Input radius:");
	scanf_s("%lf", &radius);

	circumference = 2 * pi * radius;
	area = pi * radius * radius;

	printf("printf WITHOUT width or precision specifications:\n");
	printf("circumference = %f, area = %f\n", circumference, area);
	printf("printf WITH width or precision specifications:\n");
	printf("circumference = %7.2f, area = %7.2f", circumference, area);

	/*
		Input radius:5.3
		printf WITHOUT width or precision specifications:
		circumference = 33.300854, area = 88.247263
		printf WITH width or precision specifications:
		circumference =   33.30, area =   88.25
	*/

	/*
	CONCLUSION:
	%m.n + 格式字符
	m				代表域宽			---输出数据占m个字符宽度
	.n				代表输出精度		---输出数据显示精度为n,指保留n位小数
	注:小数点也占1个字符位置
	m为正整数,当输出数据宽度小于m时,数据向右对齐,左边多余位补空格
	m为负整数,向左对齐,右边多余位补空格
	*/

	/*
	precise [prɪˈsaɪs] 
	① (ADJ-GRADED) 明确的;确切的
	You use precise to emphasize that you are referring to an exact thing, 
	rather than something vague.
	② (ADJ-GRADED) 精确的;准确的 
	Something that is precise is exact and accurate in all its details.

	precision  [prɪˈsɪʒn]
	① (N-UNCOUNT) 精确;精密 
	If you do something with precision, you do it exactly as it should be done.

	specific  [spəˈsɪfɪk]
	① (ADJ-GRADED) (部位、问题、题目等)特定的,特别的 
	You use specific to refer to a particular fixed area, problem, or subject.
	② (ADJ-GRADED) (表达)明确的,确切的,具体的 
	If someone is specific, they give a description that is precise and exact.
	You can also use specific to describe their description.

	specification  [ˌspesɪfɪˈkeɪʃn]
	① (N-COUNT) 规格;具体要求 
	A specification is a requirement which is clearly stated,
	for example about the necessary features in the design of something.
*/



/*
	函数scanf():
		scanf()函数的格式控制字符串中存在除格式说明符以外的其他字符,那么这些字符必须
		在输入数据时由用户从键盘原样输入.

	scanf()输入数值型数据时, 遇到以下几种情况都认为输入数据结束:
		(1)遇到空格符、回车符、制表符;
		(2)达到输入域宽;
        (3)遇非法字符输入;
*/



	/*
		the init part only for block2 to block4
	*/
	int a, b;

/*
	--independent block2
	%2d%2d --GETTING THE RESULT FROM  THE FOLLOWING CONCLUSION 1:
*/
	scanf_s("%2d%2d", &a, &b);
	printf("a = %d, b = %d\n", a, b);
/*
	 1234
	 a = 12, b = 34

	 CONCLUSION 1:
	 scanf()函数中:
	 %m  + 格式字符:
		按照指定宽度从输入的数据中截取所需的m个数据读入到相应变量.
		注:scanf() 没有精度修饰符.	 
*/


/*
	--independent block3 
	%d%*c%d --GETTING THE RESULT FROM  THE FOLLOWING CONCLUSION 2
*/
	//用户可以用"任意字符"作为分隔符进行数据的输入
	scanf_s("%d%*c%d", &a, &b);//%*c  读取数据进入变量时,忽略读取一个字符型数据
	printf("a = %d, b = %d\n", a, b);


/*
	--independent block4
	%2d%*2d%2d  --GETTING THE RESULT FROM  THE FOLLOWING CONCLUSION 2
*/
	//用户输入123456
	scanf_s("%2d%*2d%2d", &a, &b);
	printf("a = %d, b = %d\n", a, b);
/*
	123456
	a = 12, b = 56

	%*2d  --- 代表中间有二位输入的数值型数据被截取忽略读取,之后输入的数据开始读入到相应的变量

	CONCLUSION 2:
	scanf()函数中:
	%*m + 格式字符:
		按照指定宽度从输入的数据中截取m个数据忽略掉,后面的数据读入到相应的变量.
*/
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值