Sometimes it does matter if the compiler target for a C compiler has two's complement representation of integers or not, and having the preprocessor making the detection can be useful.
Since the standard requires the MAX/MIN macros from limits.h
and stdint.h
to be expressions that can be used in preprocessor conditionals, I think that
#include <limits.h>
#if INT_MIN + INT_MAX == -1
# define HAVE_TWOS_COMPLEMENT 1
#endif
does the trick, since one's complement and sign/magnitude architectures have symmetrical value ranges for signed integers. The question is, am I missing something here or is there a better way to make such a test in a compiler-agnostic way?