C語言資料型別佔記憶體大小

2021-09-23 01:38:54 字數 3160 閱讀 7155

c語言資料型別

使用sizeof計算各資料型別占用記憶體大小

#includeint main()

struct_test_t;

typedef enumenum_test;

typedef enumenum_test2;

typedef unionunion_test;

char a[10] = ;

short b[10] = ;

int c[10] = ;

printf("sizeof(char):%d \n",sizeof(char));

printf("sizeof(unsigned char):%d \n",sizeof(unsigned char));

printf("sizeof(short):%d \n",sizeof(short));

printf("sizeof(unsigned short):%d \n",sizeof(unsigned short));

printf("sizeof(int):%d \n",sizeof(int));

printf("sizeof(unsigned int):%d \n",sizeof(unsigned int));

printf("sizeof(long):%d \n",sizeof(long));

printf("sizeof(unsigned long):%d \n",sizeof(unsigned long));

printf("sizeof(float):%d \n",sizeof(float));

printf("sizeof(double):%d \n",sizeof(double));

printf("sizeof(char *):%d \n",sizeof(char *));

printf("sizeof(struct_test_t *):%d \n",sizeof(struct_test_t *));

printf("sizeof(struct_test_t):%d \n",sizeof(struct_test_t));

printf("sizeof(enum_test):%d \n",sizeof(enum_test));

printf("sizeof(enum_test2):%d \n",sizeof(enum_test2));

printf("sizeof(union_test):%d \n",sizeof(union_test));

printf("sizeof(char a[10]):%d \n",sizeof(a));

printf("sizeof(short b[10]):%d \n",sizeof(b));

printf("sizeof(int c[10]):%d \n",sizeof(c));

return 0 ;

}

執行結果32位編譯運算結果如下

sizeof(char):1 

sizeof(unsigned char):1

sizeof(short):2

sizeof(unsigned short):2

sizeof(int):4

sizeof(unsigned int):4

sizeof(long):4

sizeof(unsigned long):4

sizeof(float):4

sizeof(double):8

sizeof(char *):4

sizeof(struct_test_t *):4

sizeof(struct_test_t):12

sizeof(enum_test):4

sizeof(enum_test2):8

sizeof(union_test):4

sizeof(char a[10]):10

sizeof(short b[10]):20

sizeof(int c[10]):40

64位編譯運算結果如下

sizeof(char):1 

sizeof(unsigned char):1

sizeof(short):2

sizeof(unsigned short):2

sizeof(int):4

sizeof(unsigned int):4

sizeof(long):8

sizeof(unsigned long):8

sizeof(float):4

sizeof(double):8

sizeof(char *):8

sizeof(struct_test_t *):8

sizeof(struct_test_t):12

sizeof(enum_test):4

sizeof(enum_test2):8

sizeof(union_test):4

sizeof(char a[10]):10

sizeof(short b[10]):20

sizeof(int c[10]):40

總結我們一般在32/64位編譯器上使用sizeof計算各資料型別占用記憶體大小,可以得出下面結論:

char 佔1位元組

short / short int 佔2位元組

int 佔4位元組

long / long int //32位機編譯器佔4位元組,64位編譯器佔8位元組。

float 佔4位元組

double 佔8位元組

指標型別由編譯器決定 //32位機編譯器佔4位元組,64位編譯器佔8位元組。

列舉型別預設int大小,test11 =1,則占用4位元組。如果列舉成員test11 =100000000000,則占用8位元組。

結構體占用大小由成員共同決定。 (參考:另一篇blog《c語言結構體對齊問題》

聯合體占用大小類似結構體。

陣列由資料型別*陣列成員個數決定。

要知道資料型別的大小,方法就是使用sizeof親測!!!

關於資料型別佔記憶體大小的總結

計算機中的單位 儲存單位 計算機只有兩種狀態 0和1,而儲存0和1的單位就是bit。1 byte 8 bite 1位元組 8位 1 word 2 byte 1字 2位元組 byte byte,習慣上用 b 是記憶體儲存資料的基本單位。可代表乙個字元 a z 數字 0 9 或乙個符號 等。1字母 1 ...

iOS基本資料型別所佔記憶體大小

1 在swift中基本的資料型別都有哪些?float cgfloat double float t float32 float64 float80 double t int int fast8 t int fast16 t int fast32 t int fast64 t intmax t nsi...

golang 常用資料型別以及占用記憶體大小

要搞清楚這個問題先要了解幾個常用的儲存單位的轉換 1.bit 位 二進位制數中的乙個數字,可以是0或者1,是計算機中資料的最小單位。二進位制的乙個 0 或乙個 1 叫一位 2.byte 位元組 計算機中資料的基本單位,每8位組成乙個位元組 int8 8位,就是乙個位元組 int16 2個位元組 in...