Printing uint8_t variables using std::cout in C++

7.8k views Asked by At

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

There are 1 answers

9
Sergey Kalinichenko On

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;