c 语言计算数据占用字节数完整代码示例
时间: 2024-02-06 19:09:21 浏览: 17
下面是一个计算数据类型占用字节数的C语言程序示例:
```c
#include <stdio.h>
int main()
{
printf("Size of char: %d byte(s)\n", sizeof(char));
printf("Size of int: %d byte(s)\n", sizeof(int));
printf("Size of short: %d byte(s)\n", sizeof(short));
printf("Size of long: %d byte(s)\n", sizeof(long));
printf("Size of long long: %d byte(s)\n", sizeof(long long));
printf("Size of float: %d byte(s)\n", sizeof(float));
printf("Size of double: %d byte(s)\n", sizeof(double));
printf("Size of long double: %d byte(s)\n", sizeof(long double));
return 0;
}
```
该程序使用 `sizeof` 运算符来计算并打印出常见数据类型占用的字节数。运行该程序将输出类似如下的结果:
```
Size of char: 1 byte(s)
Size of int: 4 byte(s)
Size of short: 2 byte(s)
Size of long: 8 byte(s)
Size of long long: 8 byte(s)
Size of float: 4 byte(s)
Size of double: 8 byte(s)
Size of long double: 16 byte(s)
```