在函数参数pre-递增和后递增运算顺序? [英] Order of operations for pre-increment and post-increment in a function argument?

查看:106
本文介绍了在函数参数pre-递增和后递增运算顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些C code:

I have some C code:

main()
{
    int a=1;
    void xyz(int,int);

    xyz(++a,a++);     //which Unary Operator is executed first, ++a or a++?

    printf("%d",a);
}
void xyz(int x,int y)
{
    printf("\n%d %d",x,y);
}

功能 XYZ 已经过去了两个参数, ++中的 A ++ 。有人可以解释的操作顺序来解释的结果?

The function xyz has two parameters passed in, ++a and a++. Can someone explain the sequence of operations to explain the result?

以上code打印3 13或2 23,这取决于所使用的编译器。

The above code prints "3 13" or "2 23" depending on which compiler is used.

推荐答案

那么,有考虑与你的榜样code两件事情:

Well, there are two things to consider with your example code:


  1. 函数参数评估的顺序是不确定的,所以不管 ++中的 A ++ 先求是依赖于实现的。

  2. 修改的价值不止一次无不确定的行为的修改结果之间的顺序点。所以,你的code的结果是不确定的。

  1. The order of evaluation of function arguments is unspecified, so whether ++a or a++ is evaluated first is implementation-dependent.
  2. Modifying the value of a more than once without a sequence point in between the modifications results in undefined behavior. So, the results of your code are undefined.

如果我们简化您的code和删除不明确的,不确定的行为,那么我们就可以回答这个问题:

If we simplify your code and remove the unspecified and undefined behavior, then we can answer the question:

void xyz(int x) { }

int a = 1;
xyz(a++); // 1 is passed to xyz, then a is incremented to be 2

int a = 1;
xyz(++a); // a is incremented to be 2, then that 2 is passed to xyz

这篇关于在函数参数pre-递增和后递增运算顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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