|
发表于 2021-3-7 22:32:15
|
显示全部楼层
本帖最后由 墙里秋千墙外荡 于 2021-3-7 22:48 编辑
不同的编译器对于printf()函数的解释不同,有的编zhi译器解释为从左到右执行,而有的则解释为从右到左执行。对于VC6.0就是解释成从右到左执行的。
但是这并不意味着在VC6.0下执行结果就是8,7,7,8,-7,-8,出于某方面的考虑VC6.0要执行多余两个++或--后才会改变变量的值,具体的可以看汇编代码(VC6.0生成)
- ; 4 : int i=8;
- mov DWORD PTR _i$[ebp], 8
- ; 5 : printf("%d\n%d\n%d\n%d\n%d\n%d\n",++i,--i,i++,i--,-i++,-i--);
- mov eax, DWORD PTR _i$[ebp]
- neg eax
- mov DWORD PTR -8+[ebp], eax
- mov ecx, DWORD PTR -8+[ebp]
- push ecx
- mov edx, DWORD PTR _i$[ebp]
- neg edx
- mov DWORD PTR -12+[ebp], edx
- mov eax, DWORD PTR -12+[ebp]
- push eax
- mov ecx, DWORD PTR _i$[ebp]
- mov DWORD PTR -16+[ebp], ecx
- mov edx, DWORD PTR -16+[ebp]
- push edx
- mov eax, DWORD PTR _i$[ebp]
- mov DWORD PTR -20+[ebp], eax
- mov ecx, DWORD PTR -20+[ebp]
- push ecx
- mov edx, DWORD PTR _i$[ebp]
- sub edx, 1
- mov DWORD PTR _i$[ebp], edx
- mov eax, DWORD PTR _i$[ebp]
- push eax
- mov ecx, DWORD PTR _i$[ebp]
- add ecx, 1
- mov DWORD PTR _i$[ebp], ecx
- mov edx, DWORD PTR _i$[ebp]
- push edx
- push OFFSET FLAT:??_C@_0BD@PKOM@?$CFd?6?$CFd?6?$CFd?6?$CFd?6?$CFd?6?$CFd?6?$AA@ ; `string'
- mov eax, DWORD PTR _i$[ebp]
- add eax, 1
- mov DWORD PTR _i$[ebp], eax
- mov ecx, DWORD PTR _i$[ebp]
- sub ecx, 1
- mov DWORD PTR _i$[ebp], ecx
- mov edx, DWORD PTR _i$[ebp]
- add edx, 1
- mov DWORD PTR _i$[ebp], edx
- mov eax, DWORD PTR _i$[ebp]
- sub eax, 1
- mov DWORD PTR _i$[ebp], eax
- call _printf
- add esp, 28 ; 0000001cH
复制代码
所以编写程序的时候不要出现这种写法。 |
|