数据类型大小
类型 | 大小 |
---|---|
char | 1 |
short | 2 |
short int | 2 |
int | 4 |
long | 4 |
long long | 8 |
float | 4 |
double | 8 |
long double | 12 |
#include "stdio.h"
int main(void)
{
char *cp_test;
int *intp_test;
printf("size of (unsigned char) is %d\n", sizeof(unsigned char));
printf("size of (short) is %d\n", sizeof(short));
printf("size of (int) is %d\n", sizeof(int));
printf("size of (long) is %d\n", sizeof(long));
printf("size of (long long) is %d\n", sizeof(long long));
printf("size of (float) is %d\n", sizeof(float));
printf("size of (double) is %d\n", sizeof(double));
printf("size of (long double) is %d\n", sizeof(long double));
printf("size of (char pointer) is %d\n", sizeof(cp_test));
printf("size of (int pointer) is %d\n", sizeof(intp_test));
return 0;
}
运行结果:
size of (unsigned char) is 1
size of (short) is 2
size of (int) is 4
size of (long) is 4
size of (long long) is 8
size of (float) is 4
size of (double) is 8
size of (long double) is 12
size of (char pointer) is 4
size of (int pointer) is 4
数据类型转换
数据类型隐式转换
隐式转换规则
- 位数少的转换为位数多的数据结构
- 浮点数类型均先转换为double双精度数据类型,再进行运算。
- char和short类型先转换为int类型,再进行运算。
- 若参与运算量的类型不同,则先转换成同一类型,然后进行运算。
数据类型强制转换
- 位数多的强制转换为位数少的数据类型,转换后的数据类型保留低位数据。
- 存在截断效应,会导致数据精度的丢失
随笔
- 可以通过sizeof得出指针的大小,从而获得系统的位数,因为指针存储的内容为地址。