#include <cstdint>
#include <iostream>
#include <typeinfo>
int main()
std::uint8_t uint8_num = 10;
std::cout << "uint8_t num is " << uint8_num << std::endl; //无法打印
std::cout << "after cast to unsigned, uint8_t num is " << unsigned(uint8_num) << std::endl; //能正常打印
std::cout << "with a unary + operator, uint8_t num is " << +uint8_num << std::endl; //能正常打印
std::cout << "type of '+uint8_num' is " << typeid(+uint8_num).name() << std::endl;
return 0;
运行结果如下
可见使用+运算符的原理也是进行类型转换(把uint8_t 转为 int)
补充知识:
C 语言printf打印各种数据类型的方法(u8/s8/u16/s16.../u64/double/float)(全)
首先必须知道u8,s8等数据类型的定义:
typedef signed char s8;
typedef unsigned char u8;
typedef signed short s16;
typedef unsigned short u16;
typedef signed int s32;
typedef unsigned int u32;
typedef signed long long s64;
typedef unsigned long long u64;
#else /* __cplusplus */
/* Supporting _Bool in C++ is a GCC extension. */
#define _Bool bool
#if __cplusplus < 201103L
/* Defining these macros in C++98 is a GCC extension. */
#define bool bool
#define false false
#define true true
#endif
#endif /* __cplusplus */
/* Signal that all the definitions are present. */
#define __bool_true_false_are_defined 1
#endif /* stdbool.h */