How do I tell if a C integer variable is signed?
c, c-preprocessor, gcc, integer
Solution
#define ISVARSIGNED(V) ((V)<0 || (-V)<0 || (V-1)<0)
doesn't change the value of V. The third test handles the case where V == 0.
On my compiler (gcc/cygwin) this works for `int` and `long` but not for `char` or `short`.
#define ISVARSIGNED(V) ((V)-1<0 || -(V)-1<0)
also does the job in two tests.
Problem
As an exercise, I'd like to write a macro which tells me if an integer variable is signed. This is what I have so far and I get the results I expect if I try this on a char variable with gcc -fsigned-char or -funsigned-char. ``` #define ISVARSIGNED(V) (V = -1, (V < 0) ? 1 : 0) ``` Is this portable? Is there a way to do this without destroying the value of the variable?