C语言学习Day2

小理解:a=bc,计算机的理解是bc先运算,运算结果在赋值给a,运算过程与a无关.

1.浮点型数据

浮点数精度丢失
float的精度只到6-7位,可以用double是15-16.

#include <stdio.h>
#include <stdlib.h>
//防止精度丢失
int main()
{
	float a = 1.23456789e10,b;
	b = a + 20;
	printf("a=%f,b=%f\n",a,b);
	//float的精度只到6-7位,可以用double是15-16.
	double e = 1.23456789e10,c;
	c = e + 20;
	printf("e=%f,c=%f\n", e, c);
}
a=12345678848.000000,b=12345678848.000000
e=12345678900.000000,c=12345678920.000000

2、字符型数据

用char定义字符型变量,字符型变量存储字符型常量时,存储的是字符型常量对应的ASCII码

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

int main()
{
	char f = '\b';// 
	printf("\\\\\n");//  \\一个反斜杠,\n 换行
	printf("abc\rd\n");// \r 回车(让光标回到行首),得到的结果为dbc 
	printf("abc%cd\n", f);//\b退格,%c以字符形式输出
	char a, b;//用char定义字符型变量,字符型变量存储字符型常量时,存储的是字符型常量对应的ASCII码
	a = 'a';
	b = 97;
	printf("a=%c,b=%c\n", a, b);
	printf("a=%d,b=%d\n", a, b);
	a = a -32;//ASCII码小写字母减32为大写字母
	printf("a=%c\n",a);
}

注意printf(“abc\rd\n”);// \r 回车(让光标回到行首),得到的结果为dbc
printf(“abc%cd\n”, f);//\b退格,%c以字符形式输出,结果abd

\\
dbc
abd
a=a,b=a
a=97,b=97
a=A

3、混合运算

数值按int运算

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

int main()
{
	char b = 0x93 <<1>> 1;//<<1左移一位(高位丢弃,低位补0),>>1右移一位(低位丢弃,高位补0)
	printf("%x\n", b);//ffffff9,x%输出,发现最高位为1,会用f补符号位,f的二进制为1111,char为四个字节
	b = 0x93 << 1;//b储存的为0x26
	b = b >> 1;//0x13
	printf("%x\n", b);//13
}

用x%输出,char为四个字节,发现最高位为1,会用f补符号位,f的二进制为1111

ffffff93
13

防止运算时数值丢失

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

int main()
{
	long i;
	i = 131072 * 131072;
	printf("%ld\n", i);//0,运算的结果超出四个字节,将i用long long也不行,输出为0,因为在运算过程中是四个字节进行运算
       //%ld是输出长整型的数值
}
0

应该是改成

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

int main()
{
	long long i;
	i = (long long)131072 * 131072;
	printf("%lld\n", i);//%lld是输出longlong型的数值
}
17179869184

多种数据类型混合运算

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

int main()
{
	int i = 5;
	float f;
	f = i / 2;//2,不是2.5因为i为整形,先进行运算i/2得到结果为2,然后吧2赋值给f
	printf("%f\n", f);
	long l = 10;
	short s;
	f = (float)i / 2;//(类型)  强制类型转换表达式  (数据类型)表达式 单目运算符
	printf("%f\n", f);
	s = (short)1;
}

(类型) 强制类型转换表达式 (数据类型)表达式 单目运算符

2.000000
2.500000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值