[英]How compilers evaluate mathematical expressions?
I was reading this question and then I made the following. 我正在阅读这个问题,然后我做了以下内容。
a = b + (c - (b = c)) + (a - (c = a))
I tried that in C and Java. 我在C和Java中尝试过。 It works with java , but not C.
它适用于java,但不适用于C.
Of course, it depends on how the compiler evaluate such expressions and after googling about that, I failed to find the answer. 当然,这取决于编译器如何评估这些表达式,在谷歌搜索之后,我找不到答案。
The reason why it doesn't work in C is because C doesn't specify exactly when the c = a
will occur. 它在C中不起作用的原因是因为C没有准确指定
c = a
何时发生。 It can occur before or after the two other times it is referenced in that statement. 它可以在该语句中引用的另外两次之前或之后发生。
So depending on when the compiler decides to perform the assignment c = a
, the value of the expression will vary. 因此,根据编译器何时决定执行赋值
c = a
,表达式的值将有所不同。 It's not defined. 它没有定义。
ie If b = c
is evaluated before c = a
then, b
will take the original value of c
. 即如果在
c = a
之前评估b = c
那么b
将取c
的原始值。 If it is evaluated after, then it will take the value of a
. 如果是后评估,然后它会采取的值
a
。
您可以参考java 表达式文档以获得完美答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.