VC6.0 这个程序为什么输出no?

dongdandan 2013-09-10 04:34:53
if(2147483647>-2147483648)
printf("yes");
else
printf("no");
...全文
116 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
nice_cxf 2013-09-10
  • 打赏
  • 举报
回复
编译器做的常量折叠,直接把2147483647>-2147483648处理成异或了,不清楚为什么。。。 vs2008下对应汇编如下: if(2147483647>-2147483648) 00411A3E xor eax,eax 00411A40 je main+3Bh (411A5Bh)
dongdandan 2013-09-10
  • 打赏
  • 举报
回复
哦 ,原来数给抄错了! 少了一位! 结果是yes,是对的 if(2147483647>-21474836478) printf("yes"); else printf("no");
赵4老师 2013-09-10
  • 打赏
  • 举报
回复
计算机组成原理→DOS命令→汇编语言→C语言(不包括C++)、代码书写规范→数据结构、编译原理、操作系统→计算机网络、数据库原理、正则表达式→其它语言(包括C++)、架构…… 对学习编程者的忠告: 眼过千遍不如手过一遍! 书看千行不如手敲一行! 手敲千行不如单步一行! 单步源代码千行不如单步对应汇编一行! VC调试时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化,这样过一遍不就啥都明白了吗。 对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。 (Turbo C或Borland C用Turbo Debugger调试,Linux或Unix下用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
dongdandan 2013-09-10
  • 打赏
  • 举报
回复
int y=-2147483648;
  if(2147483647>-2147483647-1)
	  printf("yes");
  else
	  printf("no");
此时是yes
max_min_ 2013-09-10
  • 打赏
  • 举报
回复
引用 5 楼 dongdandan 的回复:
[quote=引用 4 楼 max_min_ 的回复:]

2147483647: int类型
-2147483648:long int 类型
比较转换成无符号类型的时候 下面的数大于上面的数
不明白,为什么-2147483648是long int 类型,4字节int的补码可以表示这个数啊? 两个数都是带符号数,为什么比较转换成了无符号数比较?[/quote] C语言中整数自动转换原则,当表达式中存在有符号和无符号类型时,所有类型自动转换成无符号类型
dongdandan 2013-09-10
  • 打赏
  • 举报
回复
引用 4 楼 max_min_ 的回复:

2147483647: int类型
-2147483648:long int 类型
比较转换成无符号类型的时候 下面的数大于上面的数
不明白,为什么-2147483648是long int 类型,4字节int的补码可以表示这个数啊? 两个数都是带符号数,为什么比较转换成了无符号数比较?
max_min_ 2013-09-10
  • 打赏
  • 举报
回复

2147483647: int类型
-2147483648:long int 类型
比较转换成无符号类型的时候 下面的数大于上面的数
赵4老师 2013-09-10
  • 打赏
  • 举报
回复
常量也有类型。 C++ Integer Constants Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types. Syntax integer-constant : decimal-constant integer-suffixopt octal-constant integer-suffixopt hexadecimal-constant integer-suffixopt 'c-char-sequence' decimal-constant : nonzero-digit decimal-constant digit octal-constant : 0 octal-constant octal-digit hexadecimal-constant : 0x hexadecimal-digit 0X hexadecimal-digit hexadecimal-constant hexadecimal-digit nonzero-digit : one of 1 2 3 4 5 6 7 8 9 octal-digit : one of 0 1 2 3 4 5 6 7 hexadecimal-digit : one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F integer-suffix : unsigned-suffix long-suffixopt long-suffix unsigned-suffixopt unsigned-suffix : one of u U long-suffix : one of l L 64-bit integer-suffix : i64 To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type. To specify a decimal constant, begin the specification with a nonzero digit. For example: int i = 157; // Decimal constant int j = 0198; // Not a decimal number; erroneous octal constant int k = 0365; // Leading zero specifies octal constant, not decimal To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example: int i = 0377; // Octal constant int j = 0397; // Error: 9 is not an octal digit To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the “x” does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example: int i = 0x3fff; // Hexadecimal constant int j = 0X3FFF; // Equal to i To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example: unsigned uVal = 328u; // Unsigned value long lVal = 0x7FFFFFL; // Long value specified // as hex constant unsigned long ulVal = 0776745ul; // Unsigned long value
dongdandan 2013-09-10
  • 打赏
  • 举报
回复
vc 6.0 用4个字节表示整数,所以表示的数的范围是-2147483648 到2147483647. 这个题改成 int x=2147483647,y=-2147483648;时,输出就是yes! 为什么?
To_be_sky 2013-09-10
  • 打赏
  • 举报
回复
有符号数和无符号数比较时,有符号数要转换为无符号数。 就是说负数要转换为整数,这个负数转化为正数是个很大的数。

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧