不明原因的行为“的printf(...,我,我++);”和变体 [英] Unexplained behavior of 'printf(..., i, ++i);' and variants

查看:134
本文介绍了不明原因的行为“的printf(...,我,我++);”和变体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/949433/could-anyone-explain-these-undefined-behaviors-i-i-i-i-i-etc\">Could谁能解释这些不确定的行为,(I = I + + + + I,I = I ++等&hellip;)

我不明白谁了以下工作:

I can't understand who the following works:

#include <stdio.h>

int main(void)

{

     int i = 1;

     printf("%d %d\n", i, i++);
     printf("%d %d\n", i++, i);
     printf("%d %d\n", i, ++i);
     printf("%d %d\n", ++i, i);

     return 0;
}

我所期望的结果如下。

I expected the following results.

1 1  \n
2 3  \n
3 4  \n
5 5  

然而,结果如下。

However, the results are as follows.

2 1 \n
2 3 \n
4 4 \n
5 5

为什么会出现这种情况?

Why does this happen?

推荐答案

这基本上是未定义的行为。编译器是免费的,以评估i和i在下面的前pression ++:

It's basically undefined behaviour. The compiler is free to evaluate i and i++ in the following expression:

f(i, i++)

在任何顺序。

完整的原因是做序列点,东西很难解释,所以我会指示你referencese的列表:

The full reason is to do with sequence points, something hard to explain, so I'll point you to a list of referencese:

未定义行为和顺序点

http://msdn.microsoft。 COM / EN-US /库/ d45c7a5d%28V = VS.80%29.aspx

http://en.wikipedia.org/wiki/Sequence_point

这篇关于不明原因的行为“的printf(...,我,我++);”和变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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