在'printf'中发布/预增量 [英] Post/pre increments in 'printf'

查看:172
本文介绍了在'printf'中发布/预增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能的重复项:

在一个语句中输出多个post和预增量

在for循环中后增量和预增量


以下代码片段

  int i = 0; 
printf(%d%d,i ++,i ++);

给出输出



1 0



我可以理解,但以下



  int i = 0 ; 
printf(%d%d,++ i,++ i);

给出输出



/ p>

有人可以解释我的第二个行为吗?

解决方案

未定义行为。请参阅:未定义的行为和序列点



引用自此链接:


总之,未定义的行为意味着
任何事情都可能发生在守护程序$ b $


对于新手:不要尝试在函数调用参数列表中修改变量值两次或更多。有关详情,请点击此处了解其含义。 : - )


Possible Duplicates:
Output of multiple post and pre increments in one statement
Post-increment and pre-increment in 'for' loop

The following code snippet

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

gives the output

1 0

I can understand that, but the following

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

gives the output

2 2

Can someone explain me the second behavior?

解决方案

Both printfs invoke undefined-behavior. See this : Undefined Behavior and Sequence Points

Quoted from this link:

In short, undefined behaviour means anything can happen from daemons flying out of your nose to your girlfriend getting pregnant.

For newbies : Don't ever try to modify values of your variables twice or more in a function call argument-list. For details, click here to know what it means. :-)

这篇关于在'printf'中发布/预增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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