mingw(/gcc)为什么会这样编译

ljq900 2007-11-22 03:06:44
devc++中用mingw编译
//若buf[i]=0x99,则tint=99
tint=(buf[i]>>4)*10+buf[i] & 0x0f;//会得到错误结果
tint=(buf[i]>>4)*10+(buf[i] & 0x0f);//正确
而在vc中两种写发没区别
似乎& 操作与+操作的优先级在vc中是&高,mingw(gcc)中是+高
...全文
97 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljq900 2007-11-26
  • 打赏
  • 举报
回复
另外vc中下面写法会有警告
10*buf[i]>>4+buf[i]&0x0f
所以每次用到位操作的时候我都加了括号
ljq900 2007-11-26
  • 打赏
  • 举报
回复
据说gcc对包含常数运算做优化中有bug。可能是bug的缘故吧
如(A+1)*10可能会优化成A*10+10(这样当然没问题),有些情况下会错误优化
我也不想查也不想记住那个优先级高,这个问题加个括号就避免了
ljq900 2007-11-26
  • 打赏
  • 举报
回复
那就是说vc不对或者不标准了?
不过我还是喜欢用括号,没什么坏处
一方面免除后顾之忧
二来看代码就不用关心那个优先级高了
kingqin 2007-11-22
  • 打赏
  • 举报
回复
在C语言中,+的优先级本来就比&高。
dielsalder 2007-11-22
  • 打赏
  • 举报
回复

int main(void)
{
int tint, t = 0x99;
tint = (t >> 4) * 10 + t & 0x0f;
printf("%d\n", tint);
return 0;
}


F:\>gcc -o test test.c

F:\>test
3

F:\>cl test.c
用于 80x86 的 Microsoft (R) 32 位 C/C++ 优化编译器 14.00.50727.42 版
版权所有(C) Microsoft Corporation。保留所有权利。

test.c
Microsoft (R) Incremental Linker Version 8.00.50727.42
Copyright (C) Microsoft Corporation. All rights reserved.

/out:test.exe
test.obj

F:\>test
3


F:\>cl test.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.20706.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

test.c
Microsoft (R) Incremental Linker Version 9.00.20706.01
Copyright (C) Microsoft Corporation. All rights reserved.

/out:test.exe
test.obj

F:\>test
3
Treazy 2007-11-22
  • 打赏
  • 举报
回复
这个到是没碰到过,楼主还真是细心呢
所以还是在表达式上加上括号
这样代码的移植性就高了!

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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