为什么这两个表达式的结果不同呢?
我当时正在回答另一个问题,但在解释 GCC 对特定表达式计算的结果时遇到了困难。 (我的答案与内联函数有关;最初的代码由两个源文件和一个标头组成。我将它们连接到一个源文件中,而不是使用内联。 )
代码
//static inline int somename(int x, int y) { return x + y + 1; }
static int somename(int x, int y) { return x + y + 1; }
extern int nothername(int x, int y);
#include <stdio.h>
int main(void)
{
printf("somename(1,2) = %d\n", somename(1, 2));
printf("nothername(2,1) = %d\n", nothername(2, 1));
return 0;
}
int nothername(int x, int y)
{
printf("x = %d, y = %d, somename(x, y) = %d, cn = %d\n",
x, y, somename(x, y), ((y != 0) ? x / y : x));
int z1 = somename(x, y) + (y != 0) ? x / y : x;
int z2 = somename(x, y) + ((y != 0) ? x / y : x);
printf("z1 = %d, z2 = %d\n", z1, z2);
return somename(x, y) + (y != 0) ? x / y : x;
}
预期输出
somename(1,2) = 4
x = 2, y = 1, somename(x, y) = 4, cn = 2
z1 = 6, z2 = 6
nothername(2,1) = 6
实际输出
somename(1,2) = 4
x = 2, y = 1, somename(x, y) = 4, cn = 2
z1 = 2, z2 = 6
nothername(2,1) = 2
问题:
- 为什么
z1
不是6?
环境
它在 MacOS X 10.6.7 上运行。我使用了 Apple 提供的 GCC 4.2.1 作为 XCode 3 的一部分;我使用的是我编译的GCC 4.6.0。最初的实验与 C++ 中的内联函数有关;使用cout
等的等效代码产生相同的计算结果。我确实检查了 somename(2, 1)
和 somename(1, 2)
都产生了 4。
I was developing an answer to another question, and ran into a brick-wall explaining the result computed by GCC on a particular expression. (My answer was related to inline
functions; originally the code consisted of two source files and a header. I've concatenated them into a single source file, not using inline
.)
Code
//static inline int somename(int x, int y) { return x + y + 1; }
static int somename(int x, int y) { return x + y + 1; }
extern int nothername(int x, int y);
#include <stdio.h>
int main(void)
{
printf("somename(1,2) = %d\n", somename(1, 2));
printf("nothername(2,1) = %d\n", nothername(2, 1));
return 0;
}
int nothername(int x, int y)
{
printf("x = %d, y = %d, somename(x, y) = %d, cn = %d\n",
x, y, somename(x, y), ((y != 0) ? x / y : x));
int z1 = somename(x, y) + (y != 0) ? x / y : x;
int z2 = somename(x, y) + ((y != 0) ? x / y : x);
printf("z1 = %d, z2 = %d\n", z1, z2);
return somename(x, y) + (y != 0) ? x / y : x;
}
Expected output
somename(1,2) = 4
x = 2, y = 1, somename(x, y) = 4, cn = 2
z1 = 6, z2 = 6
nothername(2,1) = 6
Actual output
somename(1,2) = 4
x = 2, y = 1, somename(x, y) = 4, cn = 2
z1 = 2, z2 = 6
nothername(2,1) = 2
Question:
- Why is
z1
not 6?
Environment
This is running on MacOS X 10.6.7. I've used GCC 4.2.1 provided by Apple as part of XCode 3; I've used GCC 4.6.0 which I compiled. The original experimentation was related to inline
functions in C++; equivalent code using cout
etc produces the same computational results. I did check that somename(2, 1)
and somename(1, 2)
both produce 4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
?:
的优先级非常低,因此被解释为
(4 + 1) ? (2 / 1) : 2
,即 2。这也解释了为什么在
z2
计算中添加括号可以解决问题。The precedence of
?:
is very low, sois interpreted like
which is
(4 + 1) ? (2 / 1) : 2
, which is 2.This also explains why adding the parentheses in your computation of
z2
fixed the problem.