您的位置:首页 > 编程语言 > C语言/C++

C++ signed和unsigned 大小比较

2016-10-27 10:02 501 查看
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:14px;">using namespace std;</span></span>
<span style="font-size:14px;">
int main() {
unsigned int  a = -1;
int b = 0;
if (a > b){

cout << "hello\n";
}else{
cout << "111111\n";
}
cout  << "test\n";
return 0;
}</span></span>


如上图所示输出为 hello ;a与b比较的时候,a会自动转换成unsigned int类型进行比较。由于signed int的负数最高位是1,转换成unsigned int之后,就会变成一个很大的unsigned int型正数,所以a与b比较时a会大于b。所以会输出hello;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: