VC 和 C# 真的差别到家了。。。
今天我在msnews 见到一个人说 (x++)+(++x)+(x++) 在vc 和c# 中不同,我试了一下:
VC 代码如下(MFC):
#include <iostream>
using namespace std;
void main()
{
int x = 1;
int y = (x++)+(++x)+(x++);
cout << y << endl;
cin.ignore();
}
C# 代码如下:
using System;
public class myclass {
static void Main() {
int x =1;
int y = (x++)+(++x)+(x++);
System.Console.Write(y.ToString());
System.Console.Read();
}
}
二者结果居然,VC 是6 而C# 是7
由于VC 我是用MFC 写的,没法转IL,于是我将VC 的重新写成了.net 在IL 上进行了编译,
这是VC 的IL
.method public static int32 modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl)
main() cil managed
{
.vtentry 1 : 1
// 代码大小 36 (0x24)
.maxstack 2
.locals init ([0] int32 x,
[1] int32 y)
IL_0000: ldc.i4.1
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: ldc.i4.1
IL_0004: add
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: ldloc.0
IL_0008: add
IL_0009: ldloc.0
IL_000a: add
IL_000b: stloc.1
IL_000c: ldloc.0
IL_000d: ldc.i4.1
IL_000e: add
IL_000f: stloc.0
IL_0010: ldloc.0
IL_0011: ldc.i4.1
IL_0012: add
IL_0013: stloc.0
IL_0014: ldloc.1
IL_0015: call void [mscorlib]System.Console::Write(int32)
IL_001a: call int32 [mscorlib]System.Console::Read()
IL_001f: pop
IL_0020: ldc.i4.0
IL_0021: br.s IL_0023
IL_0023: ret
} // end of method 'Global Functions'::main
这是C# 的IL
.method private hidebysig static void Main() cil managed
{
.entrypoint
// 代码大小 39 (0x27)
.maxstack 4
.locals init ([0] int32 x,
[1] int32 y)
IL_0000: ldc.i4.1
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: dup
IL_0004: ldc.i4.1
IL_0005: add
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: ldc.i4.1
IL_0009: add
IL_000a: dup
IL_000b: stloc.0
IL_000c: add
IL_000d: ldloc.0
IL_000e: dup
IL_000f: ldc.i4.1
IL_0010: add
IL_0011: stloc.0
IL_0012: add
IL_0013: stloc.1
IL_0014: ldloca.s y
IL_0016: call instance string [mscorlib]System.Int32::ToString()
IL_001b: call void [mscorlib]System.Console::Write(string)
IL_0020: call int32 [mscorlib]System.Console::Read()
IL_0025: pop
IL_0026: ret
} // end of method myclass::Main
在后面,明显多了个add,晕。。。不知有没有讨论过?