As c++ newbie, I can't fully understand why the following code doesn't work (by "doesn't work" I mean, I don't see the output 255 but instead a "broken" character):
uint8_t num = 0xff;
std::cout << num << std::endl;
I guess, num
is treated here somehow like a (non printable) character.
But I've found a code sample that solves my problem:
uint8_t num = 0xff;
std::cout << +num << std::endl;
At the moment, I have no idea what the +
operator does in this case, and I really don't know how to google this question...
I would really appreciate it if someone could point me to right direction. Many thanks in advance!
Edit:
The first part of my question is duplicate of:
uint8_t can't be printed with cout
The answer to second part of my question (why the unary +
operator helps in this case) can also be found there if you read through all answers and comments. The keyword is integer promotion