编译器错误 - 后增量&预增量运算符 [英] Compiler bug - Postincrement & Preincrement operators

查看:43
本文介绍了编译器错误 - 后增量&预增量运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的大师':


有一个简单的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

推荐答案

文章< e9 *********** ***************@posting.google.com> ;, Jinesh写道:
In article <e9**************************@posting.google.com >, Jinesh wrote:
亲爱的大师':

有一个简单的C问题。这是一个错误还是我在做什么?
真的很愚蠢?这是代码片段: -
[cut] printf(插槽%d中的元素是%d \ n,(i ++),数组[( - i)]);
Dear Guru''s:

Have a simple C questions. Is this a bug or am I doing something
reallllly stupid? Here''s the code snippet:- [cut] printf("The element in slot %d is %d\n", (i++), array[(--i)]);




您无法保证对函数的

参数进行特定的评估。例如,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


jj ****** @ hotmail.com (Jinesh)写道:
jj******@hotmail.com (Jinesh) wrote:
亲爱的大师:'br />
有一个简单的C问题。这是一个错误还是我在做什么?
真的很愚蠢?这里是代码片段: -
嗯,我远不是一个古鲁,但是让我们看看

---------- -----------------代码------------------------------ <无线电通信/> #include< stdio.h>

void main(void)
int main(void){
int i = 1;
int array [] = {0,1,2,3,4};

printf("插槽%d中的元素是%d \ n",i,array [i]);
printf(插槽%d中的元素是%d \ n,(i ++),数组[( - i)]);
返回0;}
----------------------实际输出-------------- -------------
插槽1中的元素是1
插槽0中的元素是1
----------- ----------预期输出--------------------------
插槽1中的元素为1 <插槽1中的元素是1
------------------------------------ --------------------------
输出不应该全是1'。为什么会这样?有人有线索吗?
是的:您不能保证以特定顺序传递给函数的参数

将被评估。所以,为了得到预期的结果,你需要写:


printf(" slot%d中的元素,i ++) ;

printf(" is%d \ n",array [ - i]);


一个体面的编译器会为你提供诊断您的原始代码,

如果警告级别调整正确。

操作系统: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:- Well, I''m far from being a Guru, but let'' have a look

---------------------------Code------------------------------
#include <stdio.h>

void main(void) int 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)]); return 0;}
----------------------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? Yes: you are not guaranteed in which particular order the arguments
passed to a function will get evaluated. So, to get the result you
expected, you would have to write:

printf("The element in slot %d ", i++ );
printf("is %d\n", array[--i] );

A decent compiler will give you a diagnostic for your original code,
if the warning level is adjusted properly.
Operating System: IRIX 6.5.21 (if that helps!!)

Thanks a ton in advance,
-Jin




Irrwahn

-

我可以'从这里看到它,但它看起来不错。



Irrwahn
--
I can''t see it from here, but it looks good to me.


jj ****** @ hotmail.com (Jinesh)写道:
jj******@hotmail.com (Jinesh) writes:
printf("插槽%d中的元素是%d \ n",( i ++),array [( - i)]);
printf("The element in slot %d is %d\n", (i++), array[(--i)]);




这会导致未定义的行为,因为你不止一次修改`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


这篇关于编译器错误 - 后增量&amp;预增量运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆