clanguage-lawyerpreprocessor

Should the preprocessor arithmetic match the architecture targeted by the compilation?


Let’s say I have this preprocessor check:

#if(-6 & 5)
    #error "No 2's complement signed int"
#endif

What would happen if I cross compiled from, say, a two's complement machine toward a ones' complement machine? Would the compiler use the arithmetic of the target machine, or the one of the compiling machine?


Solution

  • C 2018 6.10.1 4 says, for evaluation of expressions in #if and #elif directives:

    … For the purposes of this token conversion and evaluation, all signed integer types and all unsigned integer types act as if they have the same representation as, respectively, the types intmax_t and uintmax_t defined in the header <stdint.h>

    These types are of course used during program execution, so their behaviors are those of the execution environment.