2 lbhqfwj lbhqfwj 于 2014.05.29 10:04 提问

VS2005 编译器对warning C4307: integral constant overflow提示原则

代码:
unsigned long a = 2560*60*60*233;
unsigned long b = 0x7FFFFFFF;
unsigned long c = 0xF0000000;
//unsigned long d = 234*2560*3600; //seems beyond 0x7FFFFFFF
unsigned long temp = 1000000;
//unsigned long e = 2560*3600*360 - temp;
unsigned long f = 2560*3600*180 - temp + 2560*3600*180;
//unsigned long g = 2560*3600*360 - temp;
//unsigned long f = 2560*3600*180 - 1000000 + 2560*3600*180;
以上被注释部分是会产生编译警告的,未注释部分不会出编译警告。 I Just want to know why?

1个回答

lym753024200
lym753024200   2014.05.29 11:09

Int the expression of unsigned long d = 234*2560*3600 ,We know that the viriable of d can only represent the maximum value of 4294967295 .But the vc++ compiler consider numbers 234, 2560 and 3600 as the type of int defaultly.The int is signed , so the value changes from -2147483648 to 2147483647, but the value of 234*2560*3600 is 2156544000, which beyong the maximum value of int , so, you will get the warning above.The resolution is to add the postfix UL to 234 to make the complier regard 234UL as the type of unsigned long , then, you well find all things go as you wish.

u010402518
u010402518 不错哦
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!