怎么可能?问题是“预测输出“。 [英] How is it possibble? Question is " predict the output".

查看:68
本文介绍了怎么可能?问题是“预测输出“。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
using namespace std;

int main()
{
    if (sizeof(int) > -1)
        cout << "Yes";
    else
        cout << "No";
    return 0;
}





我的尝试:



根据我的回答应该是肯定的。

ut正确的答案是否定的。

怎么可能?



What I have tried:

according to me the answer should be yes.
ut the correct answer is no.
how is it possibble?

推荐答案

因为 sizeof 运算符返回无符号值。因此编译器然后将-1视为无符号值。十六进制值-1是0xFFFFFFFF或无符号十进制4294967295,当然远大于整数的大小(4(不是8))。在软件中混合有符号和无符号值的缺陷之一。
Because the sizeof operator returns an unsigned value. So the compiler then treats -1 also as an unsigned value. The hexadecimal value of -1 is 0xFFFFFFFF or unsigned decimal 4294967295, which of course is much greater than the size of an integer (which is 4 (not 8)). One of the pitfalls of mixing signed and unsigned values in software.


这篇关于怎么可能?问题是“预测输出“。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆