Skip to content

stdint.h

<stdint.h> 是 C99 标准引入的头文件,它提供了一些整数类型的定义,这些类型具有明确的大小和符号属性,确保在不同平台上的一致性。

引入头文件

cpp
#include <stdint.h>

数据结构

cpp
typedef signed char             int8_t;
typedef short                   int16_t;
typedef int                     int32_t;
typedef long long               int64_t;

typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;

#define INT8_MAX         127
#define INT16_MAX        32767
#define INT32_MAX        2147483647
#define INT64_MAX        9223372036854775807LL

#define INT8_MIN          -128
#define INT16_MIN         -32768
#define INT32_MIN        (-INT32_MAX-1)
#define INT64_MIN        (-INT64_MAX-1)

#define UINT8_MAX         255
#define UINT16_MAX        65535
#define UINT32_MAX        4294967295U
#define UINT64_MAX        18446744073709551615ULL

固定宽度整数类型

这些类型明确指定了其宽度(位数),确保在不同平台上具有相同的大小。

类型描述
int8_t8 位有符号整数
int16_t16 位有符号整数
int32_t32 位有符号整数
int64_t64 位有符号整数
uint8_t8 位无符号整数
uint16_t16 位无符号整数
uint32_t32 位无符号整数
uint64_t64 位无符号整数

最大值和最小值

描述
INT8_MINint8_t 的最小值
INT8_MAXint8_t 的最大值
UINT8_MAXuint8_t 的最大值
INT16_MINint16_t 的最小值
INT16_MAXint16_t 的最大值
UINT16_MAXuint16_t 的最大值
INT32_MINint32_t 的最小值
INT32_MAXint32_t 的最大值
UINT32_MAXuint32_t 的最大值
INT64_MINint64_t 的最小值
INT64_MAXint64_t 的最大值
UINT64_MAXuint64_t 的最大值
INTMAX_MINintmax_t 的最小值
INTMAX_MAXintmax_t 的最大值
UINTMAX_MAXuintmax_t 的最大值

示例

示例

cpp
#include <stdio.h>
#include <stdint.h>

int main()
{
    uint32_t a = 123;

    printf("INT8_MAX: %d\n", INT8_MAX);
    printf("INT8_MIN: %d\n", INT8_MIN);
    return 0;
}

运行结果

shell
gcc main.c -o main && ./main
INT8_MAX: 127
INT8_MIN: -128