printing numbers question

Why is the following code example not print the number eight?

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <inttypes.h>

int main()
{
	uint8_t num8 = 8;

	std::cout << "start>>" << num8 << "<<the_end" << std::endl;
}

output:
start><<the_end

expected output:
start>>8<<the_end

Thank you.
Works using printf.

http://www.cplusplus.com/reference/cinttypes/

1
2
3
4
5
6
7
8
9
10
11
#include <cstdio>
#include <inttypes.h>

int main()
{
	uint8_t num8 = 8;

	printf("start >> %u << the_end", num8);

	return 0;
start >> 8 << the_end
There is no ostream<< for uint8_t. Therefore, the compiler picks some of the existing overloads. Perhaps char.

Easy to test. What does uint8_t num8 = 'r'; print with stream?


Explicit casting of num8 to (larger) numeric type would guide the selection of the <<.
Thank you keskiverto, explicit casting worked.
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <inttypes.h>

int main()
{
	uint8_t num8 = 8;

	std::cout << "start>>" << (int)num8 << "<<end" << std::endl;
}
Last edited on
Topic archived. No new replies allowed.