问题描述
亲爱的大师':
有一个简单的C问题。这是一个错误还是我在做什么
真的很愚蠢?这是代码片段: -
--------------------------- Code- -----------------------------
#include< stdio.h>
void main(void)
{
int i = 1;
int array [] = {0, 1,2,3,4};
printf("插槽%d中的元素是%d \ n",i,array [i]);
printf("插槽%d中的元素是%d \ n",(i ++),array [( - i)]);
}
----------------------实际输出---------------------- -----
插槽1中的元素是1
插槽0中的元素是1
------- --------------预期输出--------------------------
插槽1中的元素是1
插槽1中的元素是1
-------------------- ------------------------------------------
不应该输出全部1'。为什么会这样?任何人都有一个
线索?
操作系统:IRIX 6.5.21(如果有帮助!!)
多谢提前,
-Jin
Dear Guru''s:
Have a simple C questions. Is this a bug or am I doing something
reallllly stupid? Here''s the code snippet:-
---------------------------Code------------------------------
#include <stdio.h>
void main(void)
{
int i=1;
int array[] = {0, 1, 2, 3, 4};
printf("The element in slot %d is %d\n", i, array[i]);
printf("The element in slot %d is %d\n", (i++), array[(--i)]);
}
----------------------Actual Output---------------------------
The element in slot 1 is 1
The element in slot 0 is 1
---------------------Expected Output--------------------------
The element in slot 1 is 1
The element in slot 1 is 1
--------------------------------------------------------------
Shouldn''t the output be all 1''s. Why is this happening? Anyone have a
clue?
Operating System: IRIX 6.5.21 (if that helps!!)
Thanks a ton in advance,
-Jin
推荐答案
您无法保证对函数的
参数进行特定的评估。例如,Java说,
参数应该从左到右进行评估,但C根本不会说任何关于它的任何内容。
-
Andreas K?h?ri
You''re not guaranteed a specific order of evaluation for the
arguments of functions. Java, for example, says that the
arguments should be evaluated left-to-right, but C doesn''t say
anything about it at all.
--
Andreas K?h?ri
Irrwahn
-
我可以'从这里看到它,但它看起来不错。
Irrwahn
--
I can''t see it from here, but it looks good to me.
这会导致未定义的行为,因为你不止一次修改`i'
没有插入序列点。因此对于程序行为有/任何/ b / b
是错误的。
Martin
This causes undefined behavior, because you modify `i'' more than once
without intervening sequence point. It is therefore wrong to have /any/
expectation about the program behavior.
Martin
这篇关于编译器错误 - 后增量&预增量运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!