다음과 같은 방법으로 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