为什么我不能在uint8_t类型中存储一个小整数?

use*_*112 2 c++ types

如果我将数字2存储在uint8_t类型中并尝试打印它我什么也得不到.当然一个8位整数可以存储数字2.我错过了什么?

示例代码:

#include <iostream>
#include <cstdint>

int main() {
    std::uint8_t x = 2;
    std::cout << "x = " << x << "\n";
}
Run Code Online (Sandbox Code Playgroud)

输出:

x = 
Run Code Online (Sandbox Code Playgroud)

mel*_*k47 8

uint8_t通常是一个typedef unsigned char,并将作为一个字符打印.

2 虽然是一些不可打印的字符值,所以你没有得到任何有意义的输出.

您可以使用+x将x提升为整数,并将其打印出来.