关于C编程的问题 [英] Question about C programming

查看:80
本文介绍了关于C编程的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int a, b;
a = 1;
a = a + a++;
a = 1;
b = a + a++;

printf("%d %d, a, b);

输出:3,2

有什么3号线和5之间的区别?

What's the difference between line 3 and 5?

推荐答案

A ++ 是一个修复后的运营商,它得到的,然后<值/ STRONG>增加它。

a++ is a post-fix operator, it gets the value of a then increments it.

因此​​,对于线路2,3:结果
a = 1时搜索
一个= 1 + 1,一递增。结果
一个变成3的(注意,这些操作被执行可能编译器之间变化的顺序,和一个可以很容易也成为2)

So, for lines 2,3:
a = 1
a = 1 + 1, a is incremented.
a becomes 3 (Note, the order these operations are performed may vary between compilers, and a can easily also become 2)

有关线路4,5:结果
a = 1时搜索
B = 1 + 1,一递增。结果
b换成2,变为2的(由于不确定的行为,B也可能成为++的3之前处理过的)

for lines 4,5:
a = 1
b = 1 + 1, a is incremented.
b becomes 2, a becomes 2. (Due to undefined behaviour, b could also become 3 of a++ is processed before a)

需要注意的是,除了对于理解怎么算后缀的工作外,我真的不建议使用这一招。这是未定义行为和的将会的使用不同的编译器编译时得到不同的结果

Note that, other than for understanding how postfix operators work, I really wouldn't recommend using this trick. It's undefined behavior and will get different results when compiled using different compilers

因此​​,它不仅是一种不必要的混乱的方式做事情,做起来的不可靠,最坏的实践方式。

As such, it is not only a needlessly confusing way to do things, but an unreliable, and worst-practice way of doing it.

修改:已与其他人指出,这实际上是不确定的行为

EDIT: And has others have pointed out, this is actually undefined behavior.

这篇关于关于C编程的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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