【问题标题】:Operator associavity problem with pre and post increment :( [duplicate]前后增量的运算符关联性问题:( [重复]
【发布时间】:2011-06-07 09:53:25
【问题描述】:

可能重复:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)

#include< stdio.h >

int main()
{
    int i = 1;
    int x = ++i * ++i * ++i;
    printf("%d\n", x);
    printf("%d\n\n",i);

    return 0;
}

我得到 1 的输出!!和 4 在 gcc 中。我用的是 ubuntu linux

【问题讨论】:

  • 好吧,当我使用gcc -Wall 编译时,我收到“警告:对'i' 的操作可能未定义”。您应该使用 -Wall 并注意警告。
  • 有一个 C++ 常见问题解答问题:stackoverflow.com/questions/4176328/…

标签: c gcc operator-keyword associativity


【解决方案1】:

您的代码行为未定义,因为i 在序列点之间被多次修改:

int x = ++i * ++i * ++i;

查看FAQ(我敦促您阅读整个section 3)。

【讨论】:

    【解决方案2】:

    这是未定义的行为:

    int x = ++i * ++i * ++i;
    

    别这样!!!!

    【讨论】:

      猜你喜欢
      • 2016-02-02
      • 2017-06-23
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 2023-03-26
      • 2012-11-23
      • 2012-10-17
      相关资源
      最近更新 更多