的printf(QUOT;%D \\ n"我+ +,我++)输出 [英] printf("%d %d\n",i++,i++) output
问题描述
为什么这code显示输出为3 2,而不是2 3?
`
Why does this code shows the output as "3 2" instead of "2 3" ? `
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
#include<vector>
using namespace std;
int main(){
int i=2;
printf("%d %d\n",i++,i++);
return 0;
}
`
输出是:3 2
推荐答案
在这份声明中,前pression
的printf(...)修改变量i不止一次
中间没有顺序点。
In this statement, the expression "printf(...)" modifies the variable "i" more than once without an intervening sequence point.
此行为
未定义。
This behavior is undefined.
编译器有检测的情况下
相同的变量已被修改不止一次在
一个前pression没有之间的序列点
修改。因为什么会发生改变
最后是没有定义,这当然pression可能会产生
在不同平台上不同的结果。
The compiler has detect a case where the same variable has been modified more than once in an expression without a sequence point between the modifications. Because what modification will occur last is not defined, this expression might produce different results on different platforms.
重写前pression使每
变量被修改一次。
Rewrite the expression so that each variable is modified only once.
甚至你可能会得到输出2 3,在不同的编译器
even you might get output "2 3" in different compiler
这篇关于的printf(QUOT;%D \\ n&QUOT;我+ +,我++)输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!