f(x ++)是什么意思? [英] What does f(x++) mean?

查看:366
本文介绍了f(x ++)是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准对f(x ++)有什么看法?

1. x递增,然后在函数f中使用该值为

,即x ++; f(x);

2.返回后,x(在++之前)的值被发送到f和

。 x增加,即f(x); x ++;

3.未定义的行为,对于不同的编译器是不同的


我在移植代码时遇到了问题(不是我写的)。视觉年龄和gcc给出了不同的结果(分别为2和1

),所以我不得不手动改变f(x ++);到f(x); x ++

在很多地方因为经常使用f(x ++)。

解决方案

2003年10月3日星期五07:07:41 -0700,becte写道:

标准对f(x ++)的评价是什么?
1. x递增,然后该值为
用于函数f,即x ++; f(x);
2.返回后,x(在++之前)的值被发送到f和
。 x增加,即f(x); x ++;
3.未定义的行为,对于不同的编译器是不同的

当我从AIX移植代码(不是由我编写)时,我遇到了问题。视觉年龄和gcc给出了不同的结果(分别为2和1),所以我不得不手动改变f(x ++);到f(x); x ++
在很多地方因为经常使用f(x ++)。


我刚刚在谷歌读了一个类似的问题,

从1982年开始.Dennis Ritchie回复:

来自:研究!dmr
日期:星期一12月27日06:19:21 1982
主题:纯粹和应用C

John Levine想知道是否在调用栏(x ++)中使用x全局,
栏会看到x的旧值或递增值。参数
显然是旧值;问题是在调用之后是否允许编译器进行增量。
...史蒂夫约翰逊在撰写PCC时提出了这个非常重要的观点,我说据我所知,他有权延迟增量。不过,我们同意结果
可能会给人们带来惊喜,他决定不摇摇欲坠。




至于今天,6.5.2.4.2状态


postfix ++运算符的结果是操作数的值。


所以,作为参数传递给f的值()绝对应该是增值前的价值。


6.5.2.4.2继续:


更新操作数存储值的副作用是在前一个和下一个序列点之间发生



结合C.1:


以下是5.1.2.3中描述的序列点:

- 在评估参数后调用函数。

似乎表示必须在调用

f()之前更新x的值,而不是在(2)中声明之后更新。我用GCC(3.3.1)编写了一个测试

程序来调查它的行为:


#include< stdio.h>


int x = 0;


void f(int y)

{

printf(" ;全局函数:%d \ n",x);

printf(函数中的参数:%d \ n,y);

}


int main(无效)

{

printf("在功能之前:%d \ n",x) ;

f(x ++);

printf(" after function:%d \ n",x);

返回0;

}

结果如下:


函数前:0

全局函数:1

param功能:0

功能后:1


- 谢尔顿


" becte" <他*************** @ hotmail.com>写道:

标准对f(x ++)的评价是什么?
1. x递增,然后在函数f中使用该值,即x ++; F(X);


绝对错误。这将是一个预增量效应,而不是后增量。

2.返回后,x(在++之前)的值被发送到f和
。 x增加,即f(x); X ++;


关闭。将++之前的x值发送给f。变量x是

实际上在调用f之前也会增加,因为在调用f时有一个

序列点。这不会影响发送给f的价值



3.未定义的行为,不同的编译器不同


否,行为未定义。

当我从AIX移植代码(不是由我编写)时,我遇到了问题。视觉年龄和gcc给出了不同的结果(分别为2和1),所以我不得不手动改变f(x ++);到f(x); x ++
在很多地方因为经常使用f(x ++)。




gcc给你上面的数字1?这不适合我。我得到的行为就像

我上面描述的三种不同的编译器:


C:\ prog \ c> gcc henrik.c -o henrik_gcc


C:\ prog \ c> henrik_gcc

i = 0

称为f(0)但现在i = 1 < br $>
i = 1

C:\ prog \ c> lc henrik.c -o henrik_lc.exe


C:\ prog \ c> henrik_lc

i = 0

称为f(0)但现在i = 1

i = 1


C:\ prog\c> bcc32 -ehenrik_bcc.exe henrik.c

Borland C ++ 5.5.1 for Win32版权所有(c)1993,2000 Borland

henrik.c:

Turbo Incremental Link 5.00版权所有(c)1997,2000 Borland


C:\ prog \ c> henrik_bcc

i = 0

名为f(0)但现在i = 1

i = 1

C:\ prog\c>输入henrik.c

#include< stdio.h>


int i;


void f(int a)

{

printf(f(%d),但现在i =%d \ n,a,i);

}


int main (无效)

{

printf(" i =%d \ n",i);

f(i ++);

printf(" i =%d \ n",i);

返回0;

}


-

Simon。


becte写道:

标准对f有什么看法( x ++)?
1.增加x,然后在函数f中使用该值,即x ++; f(x);
2.返回后,x(在++之前)的值被发送到f和
。 x增加,即f(x); X ++;


如果f是函数,则为
。我可能是错的,但我不认为

是任何未定义的行为。 3.未定义的行为,对于不同的编译器是不同的


如果f是一个宏,那么这个。
我在移植代码时遇到了问题(不是我写的)
从AIX到Linux。视觉年龄和gcc给出了不同的结果(分别为2和1),所以我不得不手动改变f(x ++);到f(x); x ++
在很多地方因为经常使用f(x ++)。




What does the standard say about f(x++) ?
1. x is incremented and then the that value is
used in function f, i.e. x++; f(x);
2. the value of x (before ++) is send to f and
after returning. x is increased, i.e f(x); x++;
3. undefined behavior, it is different for different compilers

I encountered the problem when I was porting code (not written by me)
from AIX to Linux. Visual age and gcc gave different results (2 and 1
respectively), so I had to manually change f(x++); to f(x); x++
in a lot of places because f(x++) was frequently used.

解决方案

On Fri, 03 Oct 2003 07:07:41 -0700, becte wrote:

What does the standard say about f(x++) ?
1. x is incremented and then the that value is
used in function f, i.e. x++; f(x);
2. the value of x (before ++) is send to f and
after returning. x is increased, i.e f(x); x++;
3. undefined behavior, it is different for different compilers

I encountered the problem when I was porting code (not written by me)
from AIX to Linux. Visual age and gcc gave different results (2 and 1
respectively), so I had to manually change f(x++); to f(x); x++
in a lot of places because f(x++) was frequently used.
I just happened to read a similar question yesterday in google,
dating from 1982. Dennis Ritchie replied:
From: research!dmr
Date: Mon Dec 27 06:19:21 1982
Subject: Pure and applied C

John Levine wondered whether, in the call bar(x++) with x global,
bar would see the old or the incremented value of x. The parameter
is clearly the old value; the question is whether the compiler might
be allowed to do the increment after the call. ... Steve Johnson brought up this very
point when he was writing PCC, and I said that as far as I knew he was
entitled to delay the increment. We agreed, though, that the result
might surprise people, and he decided not to rock the boat.



As for today, 6.5.2.4.2 states

The result of the postfix ++ operator is the value of the operand.

so, the value passed as an argument to f() should definitely be
the value before incrementing.

6.5.2.4.2 continues:

The side effect of updating the stored value of the operand shall
occur between the previous and the next sequence point.

That combined with C.1:

The following are the sequence points described in 5.1.2.3:
-- The call to a function, after the arguments have been evaluated.
Seems to indicate that the value of x must be updated before the
f() is called, not after, as you claim in (2). I compiled a test
program with GCC (3.3.1) to investigate its behavior:

#include <stdio.h>

int x = 0;

void f (int y)
{
printf("global in function: %d\n", x);
printf("param in function: %d\n", y);
}

int main (void)
{
printf("before function: %d\n", x);
f(x++);
printf("after function: %d\n", x);
return 0;
}
The results were:

before function: 0
global in function: 1
param in function: 0
after function: 1

- Sheldon


"becte" <he***************@hotmail.com> wrote:

What does the standard say about f(x++) ?
1. x is incremented and then the that value is
used in function f, i.e. x++; f(x);
Absolutely wrong. This would be a preincrement effect, not postincrement.
2. the value of x (before ++) is send to f and
after returning. x is increased, i.e f(x); x++;
Close. The value of x before the ++ is sent to f. The variable x is
also actually incremented before f is called, because there is a
sequence point at the call to f. This does not affect the value
sent to f.
3. undefined behavior, it is different for different compilers
No, the behaviour is not undefined.
I encountered the problem when I was porting code (not written by me)
from AIX to Linux. Visual age and gcc gave different results (2 and 1
respectively), so I had to manually change f(x++); to f(x); x++
in a lot of places because f(x++) was frequently used.



gcc gave you number 1 above? It doesn''t for me. I get behaviour like
what I described above for three different compilers:

C:\prog\c>gcc henrik.c -o henrik_gcc

C:\prog\c>henrik_gcc
i = 0
called f(0) but now i = 1
i = 1

C:\prog\c>lc henrik.c -o henrik_lc.exe

C:\prog\c>henrik_lc
i = 0
called f(0) but now i = 1
i = 1

C:\prog\c>bcc32 -ehenrik_bcc.exe henrik.c
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
henrik.c:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

C:\prog\c>henrik_bcc
i = 0
called f(0) but now i = 1
i = 1

C:\prog\c>type henrik.c
#include <stdio.h>

int i;

void f(int a)
{
printf("called f(%d) but now i = %d\n", a, i);
}

int main(void)
{
printf("i = %d\n", i);
f(i++);
printf("i = %d\n", i);
return 0;
}

--
Simon.


becte wrote:

What does the standard say about f(x++) ?
1. x is incremented and then the that value is
used in function f, i.e. x++; f(x);
2. the value of x (before ++) is send to f and
after returning. x is increased, i.e f(x); x++;
This one if f is a function. I could be wrong, but I don''t think there
is any undefined behavior. 3. undefined behavior, it is different for different compilers
This one if f is a macro.
I encountered the problem when I was porting code (not written by me)
from AIX to Linux. Visual age and gcc gave different results (2 and 1
respectively), so I had to manually change f(x++); to f(x); x++
in a lot of places because f(x++) was frequently used.




这篇关于f(x ++)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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