C 數據類型 長度


----數據類型長度

C99標准並不規定具體數據類型的長度大小。計算機具有不同位數的處理器,16,32和更高位的64位處理器,在這些不同的平台上,同一種數據類型具有不同的長度。

char,short,長度相同,分別為1和2個字節。

int 在32和64位處理器上皆為4個字節,在16位上是2個字節。

long在16和32位處理器上皆為4個字節,在64位上是8個字節。

long long 在16和32位處理器上皆為8個字節.

指針類型的位數與各個處理器的位數相同,分別位16,32,64位。

為了便於平台的移植,需要確保數據類型長度的一致,引用stdint.h頭文件,利用宏定義固定數據類型的長度。

typedef signed char       int8_t
typedef short int         int16_t;
typedef int               int32_t;
# if __WORDSIZE == 64
typedef long int          int64_t;
# else
__extension__
typedef long long int     int64_t;

//unsigned type is the same and omitted here

----uintptr_t,intptr_t 

指針類型的長度與處理器的位數相同,需要對指針進行運算時,利用intptr_t類型,引用stddef.h頭文件

#if __WORDSIZE == 64
typedef long int                intptr_t;
#else
typedef int                     intptr_t;
#endif
//unsigned type is the same and omitted here

----編程中要盡量使用sizeof來計算數據類型的大小

----size_t, ssize_t (in stddef.h)

Are the types size_t and uintptr_t equivalent?(http://www.viva64.com/en/k/0024/)

二者數值上相同,同處理器步長。

--in practice you can consider them equivalent and use them as you like.   

Usually size_t is used to emphasize we are dealing with a object containing same size,number of elements or iterations;   The type uintptr_t is good for pointers.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM