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

前端之家收集整理的这篇文章主要介绍了鉴于此代码段,输出CPU是否依赖?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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)为真.

猜你在找的设计模式相关文章