微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

鉴于此代码段,输出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)为真.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐