Printing uint8_t variables using std::cout in C++ [duplicate]

余生颓废 提交于 2021-02-08 10:33:50

问题


In the following code, it appears that std::cout does not print the variable of type uint8_t properly.

int main() {
    uint8_t var = 16;
    std::cout << "value: " << var << std::endl;
}

output:

value: 

I don't have problem with similar uintX_t types.

I know I'm missing something here, but I can't figure what it is.


回答1:


uint8_t maps to unsigned char on most systems. As the result, the override that interprets 16 as a non-printable character gets invoked. Add a cast to int to see the numeric value:

std::cout << "value: " << (int)var << std::endl;


来源:https://stackoverflow.com/questions/30856861/printing-uint8-t-variables-using-stdcout-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!