次のようなものに対して、どのようにenum値を設定/設定解除しますか。 GCCを使用して、私はこの迷惑な警告を受ける:暗黙的に暗黙的に符号なしタイプに変換される
test.c:37: warning: negative integer implicitly converted to unsigned type
test.c:39: warning: negative integer implicitly converted to unsigned type
test.c:41: warning: negative integer implicitly converted to unsigned type
test.c:43: warning: negative integer implicitly converted to unsigned type
コードは次のとおりです。あなたの列挙型は、任意の負の整数定数が含まれていないので
#include <stdio.h>
#include <string.h>
typedef enum {
ONE = 0x1,
TWO = 0x2,
THREE = 0x4,
FOUR = 0x8,
} options;
static const char *byte_to_binary (int x)
{
int z;
static char b[9];
b[0] = '\0';
for (z = 256; z > 0; z >>= 1)
{
strcat(b, ((x & z) == z) ? "1" : "0");
}
return b;
}
int main(int argc, char *argv[])
{
options o = 0;
printf("%s\n", byte_to_binary(o));
o |= ONE;
printf("%s\n", byte_to_binary(o));
o |= TWO;
printf("%s\n", byte_to_binary(o));
o |= THREE;
printf("%s\n", byte_to_binary(o));
o |= FOUR;
printf("%s\n", byte_to_binary(o));
o &= ~FOUR;
printf("%s\n", byte_to_binary(o));
o &= ~THREE;
printf("%s\n", byte_to_binary(o));
o &= ~TWO;
printf("%s\n", byte_to_binary(o));
o &= ~ONE;
printf("%s\n", byte_to_binary(o));
return 0;
}
行番号を表示してください。 –
これは参考になるかもしれません:http://stackoverflow.com/questions/2579230/signedness-of-enum-in-c-c99-c-cx-gnu-c-gnu-c99 – bmartins