I want to write a function (or macro) that tells whether or not a variable is signed. That is, how to tell if the given variable's type is signed or not, as opposed to its value.
You could create a _Generic macro:
#define is_signed(X) _Generic((X), \
short : true, \
int : true, \
long : true, \
long long : true, \
unsigned short : false, \
unsigned int : false, \
unsigned long : false, \
unsigned long long : false, \
float : true, \
double : true, \
long double : true \
)
You could also delegate to functions if you'd like to do something more complicated. I've made a simple example where is_signed returns a signs object including the signedness of both the type and the value of the supplied variable. I've excluded the parameter names where they are not needed, which will be allowed in the C2x standard. You can add dummy names if you want.
typedef struct { bool type; bool value; } signs;
signs Short(short x) { signs r={true, x < 0}; return r; }
signs Int(int x) { signs r={true, x < 0}; return r; }
signs Long(long x) { signs r={true, x < 0}; return r; }
signs Longlong(long long x) { signs r={true, x < 0}; return r; }
signs UShort(unsigned short) { signs r={false, false}; return r; }
signs UInt(unsigned int) { signs r={false, false}; return r; }
signs ULong(unsigned long) { signs r={false, false}; return r; }
signs ULonglong(unsigned long long) { signs r={false, false}; return r; }
signs Float(float x) { signs r={true, x < 0.f}; return r; }
signs Double(double x) { signs r={true, x < 0.}; return r; }
signs LongDouble(long double x) { signs r={true, x < 0.L}; return r; }
#define is_signed(X) _Generic((X), \
short : Short, \
int : Int, \
long : Long, \
long long : Longlong, \
unsigned short : UShort, \
unsigned int : UInt, \
unsigned long : ULong, \
unsigned long long : ULonglong, \
float : Float, \
double : Double, \
long double : LongDouble \
)(X)
You can do this for explicitly listed types using _Generic:
#include <limits.h>
#include <stdio.h>
int main(void)
{
enum Signedness { Signed, Unsigned, Unknown };
char x;
enum Signedness s = _Generic(x,
char: CHAR_MIN < 0 ? Signed : Unsigned,
signed char: Signed,
unsigned char: Unsigned,
short: Signed,
unsigned short: Unsigned,
int: Signed,
unsigned: Unsigned,
long: Signed,
unsigned long: Unsigned,
long long: Signed,
unsigned long long: Unsigned,
default: Unknown
);
switch (s)
{
case Signed:
printf("The type is signed.\n");
break;
case Unsigned:
printf("The type is unsigned.\n");
break;
case Unknown:
printf("It is not known whether the type is signed or unsigned.\n");
break;
}
}
Here is an imperfect way of doing it:
#define isSigned(x) ((x) < 0 ? 1 : (-(x) < 0 ? 1 : 0))
If the number compares less than 0, its type is obviously signed. Otherwise, it's positive, but if its negation is less than 0, again, it was signed.
But, whoops, it's not that simple, because it's also possible the value is 0! (Thanks, @JohnBollinger.) If it's exactly 0, then if x-1 is negative, it was signed.
#define isSigned(x) ((x) < 0 ? 1 : (x) > 0 ? (-(x) < 0 ? 1 : 0) : ((x) - 1 < 0 ? 1 : 0))
(Unfortunately, the extra clause to handle the x == 0 case tips it over the edge from "barely readable" to "just about completely obfuscated.)
In any case, this assumes the variable does have a value (that is, has been initialized).
Unfortunately, it doesn't work at all for types smaller than int, because in that case, -x is negative even if x was unsigned.
Demonstration:
int main()
{
char c = 1;
unsigned char uc = 1;
signed char sc = 1;
int i = -1;
unsigned u = 1;
float f = 1;
double d = -1;
printf("char: %ssigned\n", isSigned(c) ? "" : "un");
printf("signed char: %ssigned\n", isSigned(sc) ? "" : "un");
printf("unsigned char: %ssigned\n", isSigned(uc) ? "" : "un");
printf("int: %ssigned\n", isSigned(i) ? "" : "un");
printf("unsigned: %ssigned\n", isSigned(u) ? "" : "un");
printf("float: %ssigned\n", isSigned(f) ? "" : "un");
printf("double: %ssigned\n", isSigned(d) ? "" : "un");
}