1.得到内置类型的大小
#include <stdio.h>
int main(void)
{
printf("int : %d\n", sizeof(int));
printf("short : %d\n", sizeof(short));
printf("char : %d\n", sizeof(char));
printf("unsigned int : %d\n", sizeof(unsigned int));
printf("long : %d\n", sizeof(long));
printf("long long : %d\n", sizeof(long long));
printf("float : %d\n", sizeof(float));
printf("double : %d\n", sizeof(double));
printf("long double : %d\n", sizeof(long double));
return 0;
}
输出结果为:
2.得到符合类型的大小
#include <stdio.h>
struct test{
int a;
int b;
int c;
};
int main(void)
{
int a[10];
int *p;
struct test var;
printf("array: %d\n", sizeof(a));
printf("pointer: %d\n", sizeof(p));
printf("struct: %d\n", sizeof(var));
return 0;
}
结果为: