[问题] 关于C99 6.3.1.8 Usual arithmetic conve

楼主: csosmo (Be Back!)   2014-05-06 17:10:51
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC++ 2008
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
none
问题(Question):
各位专家 关于C99 6.3.1.8 Usual arithmetic conversions
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
我写了一段测试程式 我预期case2, case3的output应该要相同
(我预期都应该为1, 0, 0) 但是实际上VC++ 2008 跑出来的case2, case3结果不同
case1, 2, 3我的理解如下 再麻烦专家们指点下盲点在哪 感谢
===================================================================
case1. both two operands are converted to unsigned int (i. e. 0xffffff4)
before comparison. So, they are equal.
case2, case3: I think the results of case2, case3
should be the same as following.
step A. integer promotion
short type (0xfff4) -> int (0xfffffff4)
unsigned short -> unsigned int (0x0000fff4).
step B. arithmetic conversions => use "unsigned" type because the
rank of int and unsinged int are the same.
So, the unsigned 0xfffffff4 is larger than unsigned 0x0000fff4.
But, case2 result is not what I expect.
喂入的资料(Input):
none
预期的正确结果(Expected Output):
[1] 0, 0, 1
[2] 1, 0, 0
[3] 1, 0, 0
错误结果(Wrong Output):
[1] 0, 0, 1
[2] 0, 1, 0
[3] 1, 0, 0
程式码(Code):(请善用置底文网页, 记得排版)
int i_num = -12;
unsigned int ui_num = -12;
short s_num = -12;
unsigned short us_num = -12;
int i_num2 = s_num;
unsigned int ui_num2 = us_num;
printf("[1] %d, %d, %d\n", i_num > ui_num, i_num < ui_num, i_num == ui_num);
printf("[2] %d, %d, %d\n", s_num > us_num, s_num < us_num, s_num == us_num);
printf("[3] %d, %d, %d\n", i_num2 > ui_num2, i_num2 < ui_num2, i_num2 == ui_num2);
补充说明(Supplement):
作者: azureblaze (AzureBlaze)   2014-05-06 17:34:00
step A.错了 int可以完整表示short和ushort所以两边都promote成int
楼主: csosmo (Be Back!)   2014-05-06 17:43:00
请问哪种case会promote成unsigned int?如果case2, 两个operand都promote成int,结果为何不是相等?
作者: Fenikso (薪水小偷)   2014-05-06 18:21:00
case2.因为是-12(short)和65534(ushort)分别promote成int再拿来比较哪种case会promote成unsigned int? -> case 3
楼主: csosmo (Be Back!)   2014-05-06 20:26:00
了解, 感谢专家们解答!

Links booklink

Contact Us: admin [ a t ] ucptt.com