[Reprint] different platform sizes of data types

These things lead to cross-platform programming;

HTTPS: // www.cnblogs.com/pigerhan/p/3210255.html 

I always thought it inconsistent int type now found to be inconsistent with long size.

 

A data type related to the particular type of int bits at different machine platforms of different lengths. C99 standard does not define the specific size of the data types, only the predetermined level. For the comparison:

16 internet

char 1 byte 8
Short 2 bytes 16
int 16-bit two bytes
long 4-byte 32-bit
pointer 2-byte

32-bit platform

char 1 byte 8
short 2 bytes 16
int 32 4 bytes
Long 4 bytes
long long 8 bytes of
the pointer 4-byte

64-bit platform

char 1 byte
short 2 byte
int 4 bytes
long 8 (different) bytes
long long 8 bytes
pointer bytes 8 (difference)

II programming considerations

in order to ensure universal platform, try not to use long program database type. May use a fixed-size data type macro definitions:

typedef char Signed int8_t

typedef int int16_t Short;

int int32_t typedef;

# IF __WORDSIZE == 64
typedef int Long an int64_t;
# the else
__extension__
; typedef int Long Long an int64_t

#endif

Third, use intptr_t int may be used to ensure the universal platform, it is compiled on different platforms of different lengths, but lengths are standard platform, such as the machine 64 is its length 8 bytes, 32-bit machines whose length is 4 bytes, defined as follows:

#if __WORDSIZE == 64
typedef int Long the intptr_t;
#else
the intptr_t int typedef;
#endif
programming to make full use sizeof to calculate the size of the data type of

the above type definition has a corresponding unsigned type.

Another ssize_t and are Sign size_t size_t and unsigned signed size of computer word size. They are represented by the word length of the computer, in the 32-bit machine is an int, long type on 64-bit machines, which are identical in sense and intptr_t uintptr_t. They are defined in stddef.h inside. Note that the socket accept function uses size_t is not correct on some operating systems, because the type int * accept received, and size_t may be a long int. Later sock_t use BSD instead.

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11828857.html