目录
一、前言
二、数据类型
1、数据类型有哪些
2、为什么要有数据类型
为什么要有内置类型
为什么要有自定义类型
3、如何看待数据类型
三、sizeof – 计算不同类型变量开辟空间的大小
1、内置类型开辟的空间大小
`#include<stdio.h>
int main()
{
printf("%d\n", sizeof(char)); //1
printf("%d\n", sizeof(short)); //2
printf("%d\n", sizeof(int)); //4
printf("%d\n", sizeof(long)); //4
printf("%d\n", sizeof(long long)); //8
printf("%d\n", sizeof(float)); //4
printf("%d\n", sizeof(double)); //8
}`
2、自定义类型开辟的空间大小
数组大小
#include<stdio.h>
int main()
{
int arr1[10] = { 0 }; //40
char arr2[10] = { 0 }; //10
long int arr3[10] = { 0 }; //40
long long arr4[10] = { 0 }; //80
float arr5[10] = { 0 }; //40
double arr6[10] = { 0 }; //80
printf("%d\n", sizeof(arr1));
printf("%d\n", sizeof(arr2));
printf("%d\n", sizeof(arr3));
printf("%d\n", sizeof(arr4));
printf("%d\n", sizeof(arr5));
printf("%d\n", sizeof(arr6));
return 0;
}
其他自定义类型的大小
#include<stdio.h>
struct Test1{
int a;
char b;
float c;
double d;
};
union Test2{
int m;
char n;
};
enum Test3 {
monday,
tuesday,
wednesday,
thursday,
frifay
};
int main()
{
struct Test1 test1 = { 0 };
union Test2 test2 = { 0 };
enum Test3 test3;
printf("%d\n", sizeof(test1)); //24
printf("%d\n", sizeof(test2)); //4
printf("%d\n", sizeof(test3)); //4
}