Closed QKDV closed 2 years ago
Here is the source code.
`#include
int main() {int i=1; cout<<i<<i++<<i++<<++i<<++i<<i++<<++i<<i; }`
Classic. I would recommend sending the feedback to teathers or 院长
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就行了,没有其它保证。
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.