rai*_*ner 6 c c++ integer-promotion language-lawyer
我正在查看C++ Integer Overflow和Promotion,尝试复制它,最后结束了这个:
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
int i = -15;
unsigned int j = 10;
cout << i+j << endl; // 4294967291
printf("%d\n", i+j); // -5 (!)
printf("%u\n", i+j); // 4294967291
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在cout做什么,我阅读上述职务后预期一样,第二printf:既打印4294967291.第一printf,然而,版画-5.现在,我的猜测是,这printf只是将无符号值4294967291解释为有符号值,最终为-5(这适合看到2的补码4294967291为11 ... 11011),但我不是100 %相信我没有忽视任何事情.那么,我是对的还是其他事情发生在这里?
| 归档时间: |
|
| 查看次数: |
1039 次 |
| 最近记录: |