如何用宏检测签名?

per*_*ror 5 c signed c-preprocessor

我想,以检测是否整型家庭(的价值char,unsigned char,short,unsigned short,int,...)是C.负数.如果可能的,可以与任何兼容的C编译器(因此,没有被编译的宏gcc允许-tricks )没有任何警告!

过了一段时间,我带来了以下内容:

#define ISNEG(X) ((X) && (X-1) && ((X <= 0) && (~X >= 0)))
Run Code Online (Sandbox Code Playgroud)

我尝试了以下示例:

void
display_result(int arg, int result)
{
  printf("ISNEG(%d) is %stive\n", arg, (result ? "nega" : "posi"));
}

void
display_uresult(unsigned int arg, int result)
{
  printf("ISNEG(%u) is %stive\n", arg, (result ? "nega" : "posi"));
}

int main ()
{
  short shrt =  5;
  short nshrt = -5;
  unsigned short ushrt = 5;

  display_result(shrt, ISNEG(shrt));
  display_result(nshrt, ISNEG(nshrt));
  display_uresult(ushrt, ISNEG(ushrt));

  int ni = -5;
  int i = 5;
  int zero = 0;

  display_result(ni, ISNEG(ni));
  display_result(i, ISNEG(i));
  display_result(zero, ISNEG(zero));
  display_result(~zero, ISNEG(~zero));  // wrong

  unsigned int uzero = 0;
  unsigned int ui = 5;

  display_uresult(uzero, ISNEG(uzero));
  display_uresult(~uzero, ISNEG(~uzero));
  display_uresult(ui, ISNEG(ui));

  long int li = -5;
  unsigned long int uli = 5;

  display_result(li, ISNEG(li));
  display_uresult(uli, ISNEG(uli));

  long long int lli = -5;
  unsigned long long int ulli = 5;

  display_result(lli, ISNEG(lli));
  display_uresult(ulli, ISNEG(ulli));

  return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

结果是:

ISNEG(5) is positive
ISNEG(-5) is negative
ISNEG(5) is positive
ISNEG(-5) is negative
ISNEG(5) is positive
ISNEG(0) is positive
ISNEG(-1) is negative
ISNEG(0) is positive
ISNEG(4294967295) is positive
ISNEG(5) is positive
ISNEG(-5) is negative
ISNEG(5) is positive
ISNEG(-5) is negative
ISNEG(5) is positive
Run Code Online (Sandbox Code Playgroud)

它工作得很好,但问题是,当使用所有警告(-Wall -Wextra)编译时,我收到以下消息:

signedness.c: In function ‘main’:
signedness.c:27:3: warning: promoted ~unsigned is always non-zero [-Wsign-compare]
   display_uresult(ushrt, ISNEG(ushrt));
   ^
signedness.c:4:49: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]
 #define ISNEG(X) ((X) && (X-1) && ((X <= 0) && (~X >= 0)))
                                                 ^
signedness.c:41:26: note: in expansion of macro ‘ISNEG’
   display_uresult(uzero, ISNEG(uzero));
                          ^
signedness.c:4:49: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]
 #define ISNEG(X) ((X) && (X-1) && ((X <= 0) && (~X >= 0)))
                                                 ^
signedness.c:42:27: note: in expansion of macro ‘ISNEG’
   display_uresult(~uzero, ISNEG(~uzero));
                           ^
signedness.c:4:49: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]
 #define ISNEG(X) ((X) && (X-1) && ((X <= 0) && (~X >= 0)))
                                                 ^
signedness.c:43:23: note: in expansion of macro ‘ISNEG’
   display_uresult(ui, ISNEG(ui));
                       ^
signedness.c:4:49: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]
 #define ISNEG(X) ((X) && (X-1) && ((X <= 0) && (~X >= 0)))
                                                 ^
signedness.c:49:24: note: in expansion of macro ‘ISNEG’
    display_uresult(uli, ISNEG(uli));
                         ^
signedness.c:4:49: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]
 #define ISNEG(X) ((X) && (X-1) && ((X <= 0) && (~X >= 0)))
                                             ^
signedness.c:55:25: note: in expansion of macro ‘ISNEG’
    display_uresult(ulli, ISNEG(ulli));
                          ^
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是:

  1. 有没有更好的方法来检测我们在所有可能的整数类型的C语言中都有负变量?

  2. 如何摆脱所有这些警告而不停用它(并且不使用GCC技巧)?

jxh*_*jxh 5

这个定义似乎对我有用而没有产生任何警告:

#define ISNEG(X) (!((X) > 0) && ((X) != 0))
Run Code Online (Sandbox Code Playgroud)

我在IDEONE上使用了这个宏的测试用例.

如果你的系统支持C.11的_Generic选择功能,那么你可以做这样的事情(这有点简单,我相信它可以通过利用整体推广规则来简化):

#define ISNEG(X) \
    _Generic((X), \                 
             char: !((X) > 0) && (X) != 0, \
             signed char: (X) < 0, \
             short: (X) < 0, \
             int: (X) < 0, \
             long: (X) < 0, \
             long long: (X) < 0, \
             float: (X) < 0, \
             double: (X) < 0, \
             long double: (X) < 0, \
             default: 0)
Run Code Online (Sandbox Code Playgroud)