小编Fee*_*eez的帖子

带有无符号和有符号整数的 C++ 算术类型转换

代码:

#include<iostream>
using std::cout; using std::endl;

int main()
{
    unsigned int i = 5;
    int x = -3;

    cout << "(i + x) = " << (i + x) << endl;

    cout << "Set x to -6" << endl;
    x = -6;

    cout << "(i + x) = " << (i + x) << endl;
}
Run Code Online (Sandbox Code Playgroud)

输出:

(i + x) = 2
Set x to -6
(i + x) = 4294967295
Run Code Online (Sandbox Code Playgroud)

在这个例子中,(i + x) 返回的结果类型是一个无符号整数,但是,我认为通过算术类型转换,应该“提升”有符号整数(在这种情况下为变量“x”)在操作发生之前转换为无符号整数。这一定不是这种情况,因为 (i + x) 的第一个结果是 (5 …

c++ integer-promotion

1
推荐指数
1
解决办法
681
查看次数

标签 统计

c++ ×1

integer-promotion ×1