Improper printing of uint8_t variable

c++

Solution

`uint8_t` is not a separate data type. On systems that provide it the actual type is aliased to some standard data type, most commonly, an `unsigned char`.

Operator `<<` provides an overload that takes `unsigned char`, and prints it as a character. When you are printing your `uint8_t` variable as an `int`, cast it to an `int` for printing:

std::cout << "My ID is "<< int(myID) <<std::endl;
//                         ^^^^^

Demo.

Problem

I am trying to read a small integer value (less than 10) to a uint8_t variable. I do it like this ``` uint8_t myID = atoi(argv[5]); ``` However when I do this ``` std::cout << "My ID is "<< myID <<std::endl; ``` It prints some non-alphanumeric character. There is no issue when myID is of type int. I tried casting explicitly by doing ``` uint8_t myID = (uint8_t)atoi(argv[5]); ``` But the results are the same. Could anyone explain why this is the case and if there is any possible solution?

Original source

Related problems