当前位置 : 主页 > 手机开发 > 其它 >

鉴于此代码段,输出CPU是否依赖?

来源:互联网 收集:自由互联 发布时间:2021-06-22
void main() { if(-1 0U) printf("True\n"); else printf("False\n");} 它是依赖于处理器的(大端/小端)吗? 从 C99 6.3.1.8开始: […] Otherwise, if the operand that has unsigned integer type has rank greater or equal to the ran
void main() {

        if(-1 > 0U)
                printf("True\n");
        else
                printf("False\n");
}

它是依赖于处理器的(大端/小端)吗?

从 C99 6.3.1.8开始:

[…] Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.

因为int和unsigned int具有相同的转换级别(参见6.3.1.1),所以-1将转换为unsigned int.根据6.3.1.3,转换结果将是(-1 UINT_MAX 1)%(UINT_MAX 1)(算术说出),这显然是UINT_MAX并因此大于0.

结论是C标准要求(-1> 0U)为真.

网友评论