How to print signed char to cout as number under Android

  android, c++, cout

I’m trying to redirect a signed char to cout. It works fine under Windows/VS2019 thanks to How to output a character as an integer through cout?.

However, under Android, compiled with CLang, it does not behave as expected.

Here is the code:

char c = -1;
std::cout << "c signed is " << signed(c) << std::endl;
std::cout << "+c is " << +c << std::endl;

Under Windows, I get expected output:

c signed is -1
+c is -1

Under Android, I get expected output:

c signed is 255
+c is 255

What portable code would help displaying "-1" on all platform?

Source: Windows Questions C++

LEAVE A COMMENT