0
如何設置/取消設置類似於以下內容的枚舉值。使用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;
}
請顯示行號。 – 2012-04-23 08:38:06
這可能會有所幫助:http://stackoverflow.com/questions/2579230/signedness-of-enum-in-c-c99-c-cx-gnu-c-gnu-c99 – bmartins 2012-04-23 08:41:24