c語言中各資料型別在記憶體中的儲存方式

2021-06-21 18:01:14 字數 4214 閱讀 1657

基本的資料型別有:以32位pc為列。

char     1位元組

int        4位元組

long     4位元組

short    2位元組

signed char        1位元組

unsigned char    1位元組

unsigned long    4位元組

unsigned short   2位元組

float                    4位元組

double                8位元組

結構體的所佔位元組數計算:

typedef struct

;charcharshort 型別的結構體占4位元組記憶體。

┌───┬───┐

│ char│ char│2位元組

└───┴───┘

┌───┬───┐

│    short      │2位元組

└───┴───┘

typedef struct

short char char 型別的結構體占4位元組記憶體.

┌───┬───┐

│    short      │2位元組

└───┴───┘

┌───┬───┐

│ char│ char│2位元組

└───┴───┘

typedef struct

csc;

char short char 型別的結構提佔6位元組記憶體,以short 做2位元組對齊

┌───┬┄┄┄┐

│ char │ null │2位元組

└───┴┄┄┄┘

┌───┬───┐

│    short     │2位元組

└───┴───┘

┌───┬┄┄┄┐

│ char │ null │2位元組

└───┴┄┄┄┘

typedef struct

double char char char 型別的結構體占16位元組記憶體

8位元組┌───┬───┬───┬───┬───┬───┬───┬───┐

│                            double                                       │

└───┴───┴───┴───┴───┴───┴───┴───┘

8位元組┌───┬───┬───┬┄┄┄┬┄┄┄┬┄┄┄┬┄┄┄┬┄┄┄┐

│ char │ char │ char │                 null              │

└───┴───┴───┴┄┄┄┴┄┄┄┴┄┄┄┴┄┄┄┴┄┄┄┘

typedef struct

char char double char char 型別的結構體占24位元組記憶體

8位元組 使用2位元組空餘6位元組

┌───┬───┬┄┄┄┬┄┄┄┬┄┄┄┬┄┄┄┬┄┄┄┬┄┄┄┐

│ char │ char │                     null                     │

└───┴───┴┄┄┄┴┄┄┄┴┄┄┄┴┄┄┄┴┄┄┄┴┄┄┄┘

8位元組┌───┬───┬───┬───┬───┬───┬───┬───┐

│                            double                            │

└───┴───┴───┴───┴───┴───┴───┴───┘

8位元組 使用3位元組空餘5位元組

┌───┬───┬───┬┄┄┄┬┄┄┄┬┄┄┄┬┄┄┄┬┄┄┄┐

│ char │ char │ char │                 null                 │

└───┴───┴───┴┄┄┄┴┄┄┄┴┄┄┄┴┄┄┄┴┄┄┄┘

typedef struct

char int double char char char int 型別的結構體占用24位元組記憶體

8位元組 使用2位元組空餘6位元組

┌───┬┄┄┄┬┄┄┄┬┄┄┄┬───┬───┬───┬───┐

│ char │         null         │             int                    │

└───┴┄┄┄┴┄┄┄┴┄┄┄┴───┴───┴───┴───┘

8位元組┌───┬───┬───┬───┬───┬───┬───┬───┐

│                            double                                       │

└───┴───┴───┴───┴───┴───┴───┴───┘

8位元組 使用3位元組空餘5位元組

┌───┬───┬───┬┄┄┄┬───┬───┬───┬───┐

│ char │ char │ char │ null │             int                │

└───┴───┴───┴┄┄┄┴───┴───┴───┴───┘

typedef struct

char short int double char int float 型別的結構體占用32位元組記憶體

8位元組 使用2位元組空餘6位元組

┌───┬┄┄┄┬───┬───┬───┬───┬───┬───┐

│ char │ null │    short     │             int                    │

└───┴┄┄┄┴───┴───┴───┴───┴───┴───┘

8位元組┌───┬───┬───┬───┬───┬───┬───┬───┐

│                            double                                       │

└───┴───┴───┴───┴───┴───┴───┴───┘

8位元組 使用3位元組空餘5位元組

┌───┬┄┄┄┬┄┄┄┬┄┄┄┬───┬───┬───┬───┐

│ char │         null         │              int                   │

└───┴┄┄┄┴┄┄┄┴┄┄┄┴───┴───┴───┴───┘

8位元組 使用3位元組空餘5位元組

┌───┬───┬───┬───┬┄┄┄┬┄┄┄┬┄┄┄┬┄┄┄┐

│             float            │             null                    │

└───┴───┴───┴───┴┄┄┄┴┄┄┄┴┄┄┄┴┄┄┄┘

對上乙個結構體做位置變更,檢視記憶體占用情況

typedef struct

經過位置變更,占用記憶體為24位元組

┌───┬───┬───┬───┬───┬───┬───┬───┐

│ char│ char│    short     │             int                      │

└───┴───┴───┴───┴───┴───┴───┴───┘

┌───┬───┬───┬───┬───┬───┬───┬───┐

│             int            │            float                            │

└───┴───┴───┴───┴───┴───┴───┴───┘

┌───┬───┬───┬───┬───┬───┬───┬───┐

│                            double                                       │

└───┴───┴───┴───┴───┴───┴───┴───┘

typedef struct

short char char char int 型別的結構體占用12位元組記憶體

4位元組┌───┬───┬───┬───┐

│    short     │ char │ char│

└───┴───┴───┴───┘

4位元組 使用1位元組剩餘3位元組

┌───┬┄┄┄┬┄┄┄┬┄┄┄┐

│ char │         null         │

└───┴┄┄┄┴┄┄┄┴┄┄┄┘

4位元組┌───┬───┬───┬───┐

│             int                     │

└───┴───┴───┴───┘

c語言中各資料型別之間的強制轉換

c語言是一門弱型別程式語言,在不越界 並且可讀寫的情況下可以對一段存在記憶體空間的內容進 行任意形式的訪問及修改 對系統保留的資料型別做轉換 int main 輸出為 a這種轉換為賦值轉換,僅僅是將乙個變數的值賦予另外乙個型別的變數而已。在c語言中,char short int long float...

float,double資料型別在記憶體中的儲存方式

float在記憶體中用四個byte表示 符號位 sign 指數字 exponent 尾數 mantissa 1 bit 8 bits 23 bits 符號位 1正0負 指數字 範圍從0 255,但實際的指數等於這裡的指數減去127,所以真正的指數範圍從 127 128。尾數 23bit的尾數實際上表...

關於C語言中資料在記憶體中的儲存詳解

1.資料型別詳細介紹 2.整形在記憶體中的儲存 原碼 反碼 補碼 3.大小端位元組序介紹及判斷 4.浮點型在記憶體中的儲存解析 1.整形家族 char unsigned char signed char short unsigned short int signed short int intuns...