jmeubank / tdm-gcc

TDM-GCC is a cleverly disguised GCC compiler for Windows!
https://jmeubank.github.io/tdm-gcc/
584 stars 49 forks source link

How do old-version tdm-gcc deal with many "i++"/"++i" in one statement? #56

Closed QKDV closed 2 years ago

QKDV commented 2 years ago

image Here are the results. Left:old version(4.9.2) Right:the latest version I can understand the logic of the lastest version,but cannot understand the logic of old version. And unluckily,the exam of our school uses the old version. So I must understand it.

QKDV commented 2 years ago

Here is the source code. `#include using namespace std;

int main() {int i=1; cout<<i<<i++<<i++<<++i<<++i<<i++<<++i<<i; }`

imba-tjd commented 2 years ago

Classic. I would recommend sending the feedback to teathers or 院长

imba-tjd commented 2 years ago

It's called undefined behavior. The result is unpredicatable. You should not waste your time on this. Either is correct, which means the statement is wrong.

See https://www.zhihu.com/question/23180989

我不是要你问老师。是你说你的考试要考,我要你告诉老师这样的题目是错的。 不是通过了编译的代码就是正确的代码,比如int a; printf("%d",a);就是错误的代码,因为定义了a却没有初始化,输出结果是不确定的,但它能通过编译。 类似的还有,假设f1()会输出1,f2()会输出2,f(f1(),f2())这样调用,没有人规定会先输出1再输出2,它们执行的顺序是不确定的。就算今天先输出1,明天先输出2,对于编译器来说你也不能说它错了。但是对于写出这个语句的人,还试图找出原理,那就是大错特错。这句只要保证在运行f前先运行完f1和f2就行了,没有其它保证。