增量如何发生在printf语句用C [英] How does incrementation takes place in printf statement in C

查看:105
本文介绍了增量如何发生在printf语句用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/12529682/unexplained-behavior-of-printf-i-i-and-variants\">Unexplained行为上的printf(...,我,我++);和变种


我打印上面的线。但我得到的结果作为65.How没有我增加和打印尽管我是在第二次??

递增

  INT I = 5;
的printf(%D,我,我++);


解决方案

的printf 调用产生的未定义行为的。它是非法修改 I (在我++ ),并在同一时间执行<$ C $的一个独立的读C> I 没有插入顺序点。

不同的评价令不事在这里。所有试图解释这个code的基础上,或会发生什么的评价令的行为之前和之后发生什么事是绝对不正确。行为简直是不确定的。故事结局。

至于C语言本身而言,这code可以打印杀死所有的人!,程序崩溃,格式化你的硬盘或者干脆拒绝编译。

Possible Duplicate:
Unexplained behavior of ‘printf(…, i, ++i);’ and variants

I printed the above line. But I got the result as 65.How does I increment and print despite the fact that I is incremented at the second time??

int i=5; 
printf("%d%d",i,i++); 

解决方案

Your printf call produces undefined behavior. It is illegal to modify i (in i++) and at the same time perform an independent read of i without an intervening sequence point.

Various "orders of evaluation" do not matter here. All attempts to explain the behavior of this code based on the "orders of evaluation" or what happens "before" and what happens "after" are absolutely incorrect. The behavior is simply undefined. End of story.

As far as the C language itself is concerned, this code can print "Kill all humans!", crash the program, format your hard drive or simply refuse to compile.

这篇关于增量如何发生在printf语句用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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