为什么这两个表达式的结果不同呢?

发布于 2024-11-01 13:53:05 字数 1387 浏览 1 评论 0原文

我当时正在回答另一个问题,但在解释 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独自唱情﹋歌 2024-11-08 13:53:05

?: 的优先级非常低,因此

int z1 = somename(x, y) + (y != 0) ? x / y : x;

被解释为

int z1 = (somename(x, y) + (y != 0)) ? (x / y) : (x);

(4 + 1) ? (2 / 1) : 2,即 2。

这也解释了为什么在 z2 计算中添加括号可以解决问题。

The precedence of ?: is very low, so

int z1 = somename(x, y) + (y != 0) ? x / y : x;

is interpreted like

int z1 = (somename(x, y) + (y != 0)) ? (x / y) : (x);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文