是'Y = X = X + 1;`不确定的行为? [英] Is `y = x = x + 1;` undefined behavior?

查看:122
本文介绍了是'Y = X = X + 1;`不确定的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,是

y = x = x + 1;

用C未定义的行为?

undefined behavior in C?

推荐答案

回答您的问题结果
没有。

Answer to your question
No.

会发生什么事结果
会出现这种情况:

What will happen
This will happen:

int x = 1; /* ASSUME THIS IS SO */
y = x = x + 1;

/* Results: */
y == 2;
x == 2;

如何编译结果
一样的:

How it compiles
The same as:

x += 1;
y = x;

为什么这不是未定义结果
因为你不写 X 在相同的前pression你读它。你只需将其设置为自身 + 1 ,然后分配第X <值/ code>。

Why this is not undefined
Because you are not writing x in the same expression you read it. You just set it to itself + 1, then assign y to the value of x.

您的未来结果
如果您发现code混淆你可以使用括号可读性:

Your future
If you find the code confusing you can use parentheses for readability:

y = x = (x + 1);

这篇关于是'Y = X = X + 1;`不确定的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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