的printf(QUOT;%D%d个\\ n",++中,一个++,A)输出 [英] printf("%d %d %d\n",++a, a++,a) output

查看:116
本文介绍了的printf(QUOT;%D%d个\\ n",++中,一个++,A)输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一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;)

我不能够理解这个程序的输出(使用 GCC )。

I'm not able to understand the output of this program (using gcc).

main()
{
  int a=10;
  printf("%d %d %d\n",++a, a++,a);
}

输出:

12 10 12

此外,请解释的printf()。

推荐答案

在任意顺序正好感觉像当时的编译器将评估的printf 参数。这可能是一种优化的事情,但不能保证:他们的计算顺序不是由标准规定的,也不是实现定义。有没有办法知道。

The compiler will evaluate printf's arguments in whatever order it happens to feel like at the time. It could be an optimization thing, but there's no guarantee: the order they are evaluated isn't specified by the standard, nor is it implementation defined. There's no way of knowing.

但什么的的标准规定,是在一个操作修改同一个变量两次是未定义的行为; ISO C ++ 03,5 [EXPR] / 4:

But what is specified by the standard, is that modifying the same variable twice in one operation is undefined behavior; ISO C++03, 5[expr]/4:

在previous和下一个序列点之间的标量对象应具有其存储的值由前pression评价修改最多一次。此外,前一个值是唯一的访问以确定将被存储的值。本款的要求应符合一个完整的前pression的SUBEX pressions的每一个允许排序;否则,行为是不确定的。

Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.

的printf(%d个%D \\ n,++ A,A ++,A); 可以做一些事情;工作,你如何预期它的方式工作,你也无法理解,碰撞,爆炸,或者如果它感觉特别机智,发送讨厌的电子邮件,你的母亲(虽然这个结果在缓冲区溢出和其他类似的悲剧通常看到的)。

printf("%d %d %d\n",++a, a++,a); could do a number of things; work how you expected it, work in ways you could never understand, crash, blow up, or if it's feeling particularly witty, send nasty emails to your mother (although this result is usually seen in buffer overruns and other such tragedies).

您不应该写code这样。为您的家庭的缘故。

You shouldn't write code like this. For your family's sake.

这篇关于的printf(QUOT;%D%d个\\ n&QUOT;,++中,一个++,A)输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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