首页 > 代码库 > 无符号整型与有符号整型相运算规则

无符号整型与有符号整型相运算规则

有符号数和无符号数运算的时候,有符号数会自动向无符号数转换

技术分享
 1 #include<iostream> 2 #include<ctime> 3 #include <stdio.h> 4 #include<cstring> 5 #include<cstdlib> 6 #include <map> 7 #include <string> 8 using namespace std; 9 10 #if TEST11 int main(){12     int a = -3;13     unsigned int b = 2;14     int c = a+b;15     printf("c:%u\n", c);16     printf("c:%d\n", c);17     int x = -3;18     unsigned int y = 2;19     unsigned int z = x+y;20     printf("z:%u\n", z);21     printf("z:%d\n", z);22 }23 #endif
View Code

 

u c:4294967295
d c:-1
z:4294967295
z:-1

无符号整型与有符号整型相运算规则