-3

上記の行を印刷しました。しかし、結果は 65 になりました。

int i=5; 
printf("%d%d",i,i++); 
4

3 に答える 3

3

Your printf call produces undefined behavior. It is illegal to modify i (in i++) and at the same time perform an independent read of i without an intervening sequence point.

Various "orders of evaluation" do not matter here. All attempts to explain the behavior of this code based on the "orders of evaluation" or what happens "before" and what happens "after" are absolutely incorrect. The behavior is simply undefined. End of story.

As far as the C language itself is concerned, this code can print "Kill all humans!", crash the program, format your hard drive or simply refuse to compile.

于 2012-12-10T08:32:22.023 に答える
1

ANSI C99 ISO/IEC 9899:1999 標準は次のように述べています。

6.5.2.2 関数呼び出し 関数指定子、実引数、および実引数内の部分式の評価順序は規定されていませんが、実際の呼び出しの前にシーケンス ポイントがあります。

于 2012-12-10T08:29:46.367 に答える
0

先ほど発見したように、評価の順序は指定されていません。コンパイラは、任意の順序で引数を自由に評価できます。(あなたの場合、 i++ は i の前に評価されます。)

于 2012-12-10T08:28:14.827 に答える