gpt4 book ai didi

c# - 一元 : Why unary's behavior in c# varies with c/c++

转载 作者:太空狗 更新时间:2023-10-29 20:17:01 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Undefined, unspecified and implementation-defined behavior
Undefined Behavior and Sequence Points
Pre & post increment operator behavior in C, C++, Java, & C#

我有这个代码片段:

int x = 2;
int y = x + 4 * ++x;
// what is y???

当我在 c/c++ 中编译和测试它时,我会得到:

// C/C++
y is 15

但是通过 c# 我会得到

// C#
y is 14

为什么?


IL 的一部分是:

locals init ([0] int32 x,
[1] int32 y)
IL_0000: nop
IL_0001: ldc.i4.2
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: ldc.i4.4
IL_0005: ldloc.0
IL_0006: ldc.i4.1
IL_0007: add
IL_0008: dup
IL_0009: stloc.0
IL_000a: mul
IL_000b: add
IL_000c: stloc.1
IL_000d: ldloca.s y

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com