栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言基础学习(2)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言基础学习(2)

1、浮点数

  表示带小数点的数值

int表示定义一个整数,占位4位  %d

float表示定义一个单精度浮点数   %f

double表示定义一个浮点数,双精度浮点数 占位64位  %lf

#include           
int main()                   
{
	double foot;
	double inch;
	printf("请输入身高英尺和英寸:");
	scanf("%lf %lf",&foot,&inch);
	printf("身高是%lf米n",((foot+inch/12)*0.3048));

	return 0;
}

输出结果:

请输入身高英尺和英寸:5 7
身高是1.701800米
请按任意键继续. . .

2、表达式

求两个时间之差:

#include           
int main()                   
{
	int hour1,minute1;
	int hour2,minute2;
	int hour3,minute3;
	printf("请输入第一个时间:");
	scanf("%d %d",&hour1,&minute1);
	printf("请输入第二个时间:");
	scanf("%d %d",&hour2,&minute2);

	hour3 = (hour2*60+minute2-hour1*60-minute1)/60;
	minute3 = (hour2*60+minute2-hour1*60-minute1)%60;
	printf("两个时间的差是%d小时%d分钟n",hour3,minute3);
	return 0;
}

输出结果:

请输入第一个时间:2 50
请输入第二个时间:4 30
两个时间的差是1小时40分钟
请按任意键继续. . .

3、运算符优先级

+ - 单目运算 自右向左   

= 赋值  也是自右向左   a = 6; a被赋值6  ;  a = b = 6  --->a = (b = 6)     

其他自左向右

4、交换两个变量

#include           
int main()                   
{
	int a= 5;
	int b = 6;
	int t = 0;

	t = a;   //把a的值存放到t里面、
	a = b;   //把b的值赋给a
	b = t;  //把t的值给b
	printf("a=%d b=%dn",a,b);
	return 0;
}

输出结果:

a=6 b=5
请按任意键继续. . .

调试:插入断点(表示即将进行该行的运算)

5、复合赋值和递增递减

total +=5 ------------------>total = total+5;

count++------------------>count+=1------------>count = count+1

前缀 ++count   a+1以后的值

后缀  count++ 的值是a加1以前的值

无论哪个a自己的值都加了1

#include           
int main()                   
{
	int a= 10;
	
	printf("a++=%dn",a++);
	printf("a=%dn",a);

	printf("++a=%dn",++a);
	printf("a=%dn",a);
	return 0;
}

输出结果:

a++=10
a=11
++a=12
a=12
请按任意键继续. . .

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/994009.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号