加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 04:59:27 所属栏目:百科 来源:网络整理
导读:void main() { if(-1 0U) printf("Truen"); else printf("Falsen");} 它是依赖于处理器的(大端/小端)吗? 解决方法 从 C99 6.3.1.8开始: […] Otherwise,if the operand that has unsigned integer type has rank greater or equal to the rank of the ty
void main() {

        if(-1 > 0U)
                printf("Truen");
        else
                printf("Falsen");
}

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

解决方法

从 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)为真.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读