第17节 赋值运算符及表达式

一.简单的赋值表达式

#include <stdio.h>
#include <math.h>
int main()
{
	int a, b;
	a = 3;
	double c, d;
	b = a * a;
	c = 3.75;
	d = sin(b) + 1;
}

二.赋值过程中的数据类型自动转换

左值右值转换方法举例
整型变量浮点型数据浮点数舍弃其小数部分int a=4.38;
浮点型变量整型数据数值不变,以指数形式存储double=375;
float变量double数据精度降低,可能溢出float f=2.8e59
unsigned型变量长度相同的signed型数据原样照搬,但原符号位也作为数值部分对待unsigned int u=-32765;
#include <stdio.h>
int main()
{
	int i = 3.123456789;
	double d = 3;
	float f = 3.123456789123456789;
	unsigned u1 = 1234;
	unsigned u2 = -1234;
	printf("i=%d\n", i);
	printf("d=%f\n", d);
	printf("f=%f\n", f);
	printf("u1=%u\n", u1);
	printf("u2=%u\n", u2);
}
输出结果及解析:
i=3           /舍弃小数部分
d=3.000000    /整数转为小数存储和输出
f=3.123457    /精度降低
u1=1234       /值未变
u2=4294966062 /符号位当数值处理

三.复合的赋值运算符

#include <stdio.h>
int main()
{
	int a = 9;
	a += 3; printf("%d\n", a); a = 9;
	a -= 3; printf("%d\n", a); a = 9;
	a *= 3; printf("%d\n", a); a = 9;
	a /= 3; printf("%d\n", a); a = 9;
	a %= 3; printf("%d\n", a); a = 9;
}
输出结果:
12
6
27
3
0

复合的赋值运算符优点
①简化程序
②提高效率
四.赋值表达式及其值
赋值语句:
a=3;
b+=6.32;
赋值表达式:
a=3
b+=6.32

 #include <stdio.h>
int main()
{
	int a, b, c;
	printf("%d\n", (a = 5));
	printf("%d\n", (b = (c = 6)));
	printf("%d %d %d\n", a, b, c);
}
5
6
5 6 6

五.赋值表达式求值

结合方式:由右向左

a=(b=5)
a=b=c=5
a=5+(c=6)
a%=(n%=2)
printf("%d",(j=i++);
(a=3*5)=4*3
a+=a-=a*a

六.点击自测

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值