typedef

typedef是 C 语言中的关键字,直译可以理解为 类型定义。

C

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;

没有无符号的浮点数。

char 1字节 uint8_t 1字节 uint16_t 2字节 uint32_t 4字节 uint64_t 8字节

  1. 定宽整数类型
类型 描述 示例值
int8_t 8位有符号整数 -128 ~ 127
uint8_t 8位无符号整数 0 ~ 255
int16_t 16位有符号整数 -32768 ~ 32767
uint16_t 16位无符号整数 0 ~ 65535
int32_t 32位有符号整数 -2147483648 ~ 2147483647
uint32_t 32位无符号整数 0 ~ 4294967295U
int64_t 64位有符号整数 -9223372036854775808LL ~ 9223372036854775807LL
uint64_t 64位无符号整数 0 ~ 18446744073709551615ULL

如果是

C++14

C#

Python

python中没有typedef关键字,因为Python是一种动态类型语言,不需要像C/C++一样进行类型定义。

容易造成死循环

如果在for 循环当中特定类型的变量,有可能造成死循环,如下代码


    uint8_t i=0;
    
    for(i=0;i<500;i++){
        printf("i=%d \n", i);
    }

此段代码其实就是实际上的死循环。i 值永远不可能大于 500,因此程序无法退出。

显然,当我们使用低精度的数据类型时,必须确定所使用的范围。

参考链接