Definitions of data types such as int8_t int16_t int32_t

Keywords: Database Programming

1. Data types, especially int-related types, have different lengths under different bit machines. The C99 standard does not specify the length and size of specific data types, but only the level:
16-bit platform
1 byte, 8 bits
2 bytes, 16 bits
int) 2 bytes 16 bits
4 bytes, 32 bits
Pointer 2 bytes

32-bit platform
1 byte, 8 bits
2 bytes, 16 bits
4 bytes 32 bits
long 4 bytes
long long 8 bytes
Pointer 4 bytes

64-bit platform
char * 1 byte
2 bytes
int * 4 bytes
long 8 bytes (difference)
long long 8 bytes
Pointer 8 bytes (difference)

In order to ensure the universality of the platform, the long database type should not be used in the program as far as possible. You can use fixed size data type macro definitions

When using int, intptr_t can also be used to ensure the universality of the platform. It compiles on different platforms with different lengths, but all of them are standard platform lengths. For example, 64-bit machines have 8 bytes in length and 32-bit machines have 4 bytes in length, which are defined as follows:

#if __WORDSIZE == 64
typedef long int                intptr_t;
#else
typedef int                       intptr_t;
#endif

The above type definitions all have corresponding unsigned types.


Use sizeof as much as possible to calculate the size of data type in programming

size_t :   unsigned signed size of computer word size.
ssize_t: sign size_t
They also represent the word length of a computer. They are int on 32-bit machines and long on 64-bit machines. In a sense, they are equivalent to intptr_t and uintptr_t. They are defined in stddef.h.

 

In stdint.h:

/* There is some amount of overlap with <sys/types.h> as known by inet code */
#ifndef __int8_t_defined
# define __int8_t_defined
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;
# endif
#endif

/* Unsigned.  */
typedef unsigned char        uint8_t;
typedef unsigned short int    uint16_t;
#ifndef __uint32_t_defined
typedef unsigned int        uint32_t;
# define __uint32_t_defined
#endif
#if __WORDSIZE == 64
typedef unsigned long int    uint64_t;
#else
__extension__
typedef unsigned long long int    uint64_t;
#endif

 

There are also maxima of these types:

 

/* Minimum of signed integral types.  */
# define INT8_MIN        (-128)
# define INT16_MIN        (-32767-1)
# define INT32_MIN        (-2147483647-1)
# define INT64_MIN        (-__INT64_C(9223372036854775807)-1)
/* Maximum of signed integral types.  */
# define INT8_MAX        (127)
# define INT16_MAX        (32767)
# define INT32_MAX        (2147483647)
# define INT64_MAX        (__INT64_C(9223372036854775807))

/* Maximum of unsigned integral types.  */
# define UINT8_MAX        (255)
# define UINT16_MAX        (65535)
# define UINT32_MAX        (4294967295U)
# define UINT64_MAX        (__UINT64_C(18446744073709551615))

From: https://www.cnblogs.com/zijianlu/archive/2013/06/05/3118638.html

Posted by djjjozsi on Wed, 24 Jul 2019 03:28:43 -0700