1.判断数据类型的小技巧
#include<stdio.h>
int main()
{
int* p;//整型指针变量(无法与基本类型相互赋值)
char a;
short b;
int c;
long d;
float e;
double f;
p = a;
p = b;
p = c;
p = d;
p = e;
p = f;
return 0;
}
运行结果:(可以通过报错确定数据类型)
2.类型之间运算会有什么变化?
1.整型同类型之间运算
(1)有符号位的同类型运算
#include<stdio.h>
int main()
{
int* p;
char a;
short b;
int c;
long d;
p=a+a;
p=b+b;
p=c+c;
p=d+d;
return 0;
}
运行结果:
可以看出:比int等级低的数据类型运算,会变成int类型
(2)无符号位的同类型运算
#include<stdio.h>
int main()
{
int* p;
unsigned char a;
unsigned short b;
unsigned int c;
unsigned long d;
p=a+a;
p=b+b;
p=c+c;
p=d+d;
return 0;
}
运行结果:
可以看出:比int等级低的数据类型运算,都会变成int类型
2.整型不同类型之间运算
(1)有符号位的不同类型运算
#include<stdio.h>
int main()
{
int* p;
char c;
short s;
int i;
long l;
p=c+s;
p=s+i;
p=i+l;
return 0;
}
运行结果:
可以看出:若运算时两个类型均低于或等于int类型,那么结果是int类型;
若运算时类型有高于int类型,那么结果是其中等级最高的类型。
(2)无符号位的不同类型运算
#include<stdio.h>
int main()
{
int* p;
unsigned char c;
unsigned short s;
unsigned int i;
unsigned long l;
p=c+s;
p=s+i;
p=i+l;
return 0;
}
运行结果:
可以看出:若运算时两个类型均低于或等于int类型,那么结果是int类型;
若运算时类型有高于int类型,那么结果是其中等级最高的类型。
(3)混合运算
#include<stdio.h>
int main()
{
int* p;
char c;
short s;
int i;
long l;
unsigned char uc;
unsigned short us;
unsigned int ui;
unsigned long ul;
p=c+uc;
p=s+us;
p=i+ui;
p=l+ul;
return 0;
}
最终结论:若运算时两个类型均低于或等于int类型,那么结果是int类型;
若运算时类型有高于int类型,那么结果是其中等级最高的类型。
3.浮点型同类型之间运算
#include<stdio.h>
int main()
{
int* p;
float a;
double b;
p=a+a;
p=b+b;
return 0;
}
可以看出:浮点类型同类型运算,类型保持不变
4.浮点型不同类型之间运算
#include<stdio.h>
int main()
{
int* p;
float a;
double b;
p=a+;
return 0;
}
可以看出:运算结果为其中的最高等级
5.整型浮点型混合运算
#include<stdio.h>
int main()
{
int* p;
long l;
unsigned long ul;
float f;
double d;
p=l+f;
p=ul+d;
return 0;
}
可以看出:浮点型等级比整型高。
3.整型浮点型等级排序
//由低到高
char
unsigned char
short
unsigned short
int
unsigned int
long
unsigned long
float
double
4.强制类型转换
(类型)需要转换的数据对象
#include<stdio.h>
int main()
{
int a=5;
int b=2;
printf("%f\n",(float)a/b);
printf("%f\n",a/(double)b);
return 0;
}