C:为什么当GCC从右到左计算时,LLVM从左到右评估printf? [英] C: why does LLVM evaluate printf left-to-right when GCC evaluates right-to-left?

查看:520
本文介绍了C:为什么当GCC从右到左计算时,LLVM从左到右评估printf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如此问题所述: LLVM和GCC,不同的输出相同的代码,LLVM和GCC对于同一代码产生不同的输出。

As stated in this question: LLVM and GCC, different output same code, LLVM and GCC result in different output for the same code.

#include <stdio.h>

#define MAX(a,b) ( (a) > (b) ? (a) : (b) )

int increment() {
    static int i = 42;
    i += 5;
    printf("increment returns %d\n",i);
    return i;
}

int main( int argc, char ** argv ) {
    int x = 50;
    printf("max of %d and %d is %d\n", x,increment(),MAX(x, increment()));
    printf("max of %d and %d is %d\n", x,increment(),MAX(x, increment()));
    return 0;
}

LLVM输出提供:

increment returns 47
increment returns 52
increment returns 57
max of 50 and 47 is 57
increment returns 62
increment returns 67
increment returns 72
max of 50 and 62 is 72

,而GCC输出给出:

increment returns 47
increment returns 52
max of 50 and 52 is 50
increment returns 57
increment returns 62
increment returns 67
max of 50 and 67 is 62

现在,在另一个问题中,有人回答说,没有指定参数的求值顺序,这就是为什么行为是未指定的。但是,如果仔细阅读代码,您可以看到,实际上评估的顺序IS是指定的。 GCC编译器从 printf()中评估 x,increment(),MAX(x,increment())方法从右到左,而LLVM编译器从左到右评估此代码。有谁知道为什么会是这样?不应该像 printf 的评估顺序清楚地定义,并且对于两个编译器都是一样的

Now, in the other question, someone answered that the order of evaluation of the parameters is not specified which is why the behavior is unspecified. However, if you go through the code very carefully, you can see that in fact the order of evaluation IS specified. The GCC compiler is evaluating x,increment(),MAX(x, increment()) from the printf() method right-to-left, while the LLVM compiler is evaluating this code left-to-right. Does anyone know why this is the case? Shouldn't something like the order of evaluation of printf be clearly defined and the same for both compilers??

此外,我只想澄清一下,这段代码是从一个教程,旨在了解C语言如何工作。它的目的不是一个正常工作或具有准确输出的程序。古怪的输出是有意的,所以使用一个傻的宏,如代码(MAX)中使用的宏。我只是想了解为什么这里有这么大的区别,谢谢!

Also, I just want to clarify that this code is from a tutorial and is aimed at understanding how the C language works. Its purpose is not to be a program that works properly or to have output that is accurate. The wacky output is intentional and so is the use of a silly macro like the one used in the code (MAX). I'm only trying to understand why there is such a big difference here, thanks!

推荐答案

函数参数的评估顺序未由规范定义。编译器可以以任意顺序自由评价。只是因为某个特定的实现以一致的方式进行,并不改变两个不同实现可能具有不同行为的事实。

The evaluation order of function arguments isn't defined by the specification. Compilers are free to evaluate in whatever order they like. Just because a particular implementation happens to do it in a consistent way doesn't change the fact that two different implementations might have different behaviour.

如果你想要不同的输出编译器,您需要编写具有根据标准定义好的行为的代码。

If you want consistent output from different compilers, you need to write code that has well-defined behaviour according to the standard.

这篇关于C:为什么当GCC从右到左计算时,LLVM从左到右评估printf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆