是C签名或未签名的枚举?

时间:2015-03-11 02:38:30

标签: c enums unsigned

是否在C中签名或未签名?我使用Gcc编译我的程序,我得到2个枚举值的结果如下代码:

typedef enum test_e
{
    zero,
    one,
    two,
    three,
}test_t;

void compare_func(test_t var)
{
    printf("\n [1] my number is %d \n", var);

    if ( var > 0)
        printf(" %d > 0 \n", var);
    else if ( var < 0)
        printf(" %d < 0 \n", var);
}

typedef enum test_nega_e
{
    zero_nega,
    one_nega = -1,
    two_nega = -2,
    three_nega = -3,
}test_nega_t;

void compare_nega_func(test_nega_t var_nega)
{
    printf("\n [2] my number_nega is %d \n", var_nega);
    if ( 0 < var_nega)
        printf(" %d > 0 \n", var_nega);
    else if ( 0 > var_nega)
        printf(" %d < 0 \n", var_nega);
}

int main (void)
{
    test_t my_number;
    test_nega_t my_number_nega;

    my_number = -1;
    compare_func(my_number);

    my_number_nega = -1;
    compare_nega_func(my_number_nega);

    return 0;
}

结果是:

 [1] my number is -1 
 -1 > 0 

 [2] my number_nega is -1 
 -1 < 0 

所以&#34; my_number&#34;是未签名的&#34; my_number_var&#34;签了?感谢

1 个答案:

答案 0 :(得分:0)

您不能依赖已签名或未签名的枚举。

请参阅Are C++ enums signed or unsigned?

请注意C和C ++枚举类似