C 语言编程 — uint8_t / uint16_t / uint32_t /uint64_t

目录

uint8_t\uint_16_t\uint32_t\uint64_t

在 C99 标准(ISO C99: 7.18 Integer types)的 stdint.h 头文件中通过 typedef 定义了这些数据类型:

#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  

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  

可以看出定义 uint8_t / uint16_t / uint32_t /uint64_t 的目的是为了跨平台编程的可移植性。

格式化输出

  • uint16_t - %hu
  • uint32_t - %u
  • uint64_t - %llu
  • uint8_t - 从定义中可以看出,uint8_t 实际上是一个 char 类型,所以在输出 uint8_t 类型的变量时,实际上输出了其对应的字符,而不是数值:
uint8_t num = 67;
cout << num << endl;

// 输出结果:C

猜你喜欢

转载自blog.csdn.net/Jmilk/article/details/106452714