为什么下面这段代码先写B2,然后写A1?它不应该同时编写两个A1吗?C++中的隐式数据类型从有符号整型转换为无符号整型(在层次结构中更高)
short a=-5;
unsigned short b=-5u;
if(a==b)
printf("A1");
else
printf("B2");
// prints B2
int a2=-5;
unsigned int b2=-5u;
if(a2==b2)
printf("A1");
else
printf("B2");
return 0;
// prints A1发布于 2013-02-18 01:34:52
将负有符号整数类型转换为unsigned总是下溢,并产生模运算。unsigned int x = (unsigned int)-1将UINT_MAX存储到x中。
Example
unsigned int x = (unsigned int) -1;
std::cout << x << std::endl;
x = (unsigned int) -5;
std::cout << x << std::endl;输出:
4294967295
4294967291请注意,-1和-5都已转换为极高的值,其差值也等于4。
https://stackoverflow.com/questions/14923825
复制相似问题