I have this very simple C++ code :
#include <iostream>
int main() {
std::cout << (int)(char)(int)char(-2);
return 0;
}
I am executing it on different setups :
I want to obtain -2 all the time, and I don't understand this difference in behavior.
Does anyone have any idea?
A compiler is allowed to choose if char
is signed or unsigned. The standard says that they have to pick, but don't mandate which way they choose.
GCC supports -fsigned-char
and -funsigned-char
to force this behavior.