C语言基本数据类型占用的字节数可以通过如下例子获取:
#include<stdio.h> int main(void) { printf("char size=%d \n",sizeof(char)); printf("int size=%d \n",sizeof(int)); printf("long size=%d \n",sizeof(long)); printf("float size=%d \n",sizeof(float)); printf("double size=%d \n",sizeof(double)); printf("char* size=%d \n",sizeof(char*)); printf("int* size=%d \n",sizeof(int*)); printf("long* size=%d \n",sizeof(long*)); printf("float* size=%d \n",sizeof(float*)); printf("double* size=%d \n",sizeof(double*)); printf("char[] size=%d \n",sizeof(char[2])); return 0; }
执行结果:
$ ./size.exe
char size=1
int size=4
long size=8
float size=4
double size=8
char* size=8
int* size=8
long* size=8
float* size=8
double* size=8
char[] size=2
以上,单位是字节,一个字节为8比特
其中需要注意的是任何类型的指针变量占用8个字节
时间: 2024-10-14 04:35:22