整数型ファミリ ( char
、unsigned char
、short
、unsigned short
、int
...) の値が C で負の数であるかどうかを検出しようとしています。可能であれば、対応する C コンパイラでコンパイルできるマクロを使用します (したがって、gcc
トリックは許可されません)。 )そして警告なし!
しばらくして、次のものが届きました。
#define ISNEG(X) ((X) && (X-1) && ((X <= 0) && (~X >= 0)))
次の例で試しました。
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;
}
そして、結果は次のとおりです。
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
それは非常にうまく機能しますが、問題は、すべての警告 ( -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));
^
だから、私の質問は次のとおりです。
C言語の可能なすべての整数型の中に負の変数があることを検出するより良い方法はありますか?
非アクティブ化せずに (そして GCC トリックを使用せずに) これらすべての警告を取り除くにはどうすればよいですか?