Search code examples
c++arm

C++ convertion char int ARM


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 :

  • Ubuntu 20.04 x64 (g++) -> Output : -2
  • Windows 10 x64 MSVC 2017 -> Output : -2
  • MacOS x64 Clang -> Output -2
  • Ubuntu Armv8 (g++) / EC2 Instance -> Output : 254

I want to obtain -2 all the time, and I don't understand this difference in behavior.

Does anyone have any idea?


Solution

  • 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.