#include <stdio.h>

int main(void)
{
   int i = 0;
   i = i++ + ++i;
   printf("%d\n", i); // 3

   i = 1;
   i = (i++);
   printf("%d\n", i); // 2 Should be 1, no ?

   volatile int u = 0;
   u = u++ + ++u;
   printf("%d\n", u); // 1

   u = 1;
   u = (u++);
   printf("%d\n", u); // 2 Should also be one, no ?

   register int v = 0;
   v = v++ + ++v;
   printf("%d\n", v); // 3 (Should be the same as u ?)

   int w = 0;
   printf("%d %d\n", ++w, w); // shouldn't this print 1 1

   int x[2] = { 5, 8 }, y = 0;
   x[y] = y ++;
   printf("%d %d\n", x[0], x[1]); // shouldn't this print 0 8? or 5 0?
}

当前回答

我认为C99标准的相关部分是6.5表达式,§2

在前一个序列点和下一个序列点之间,一个对象应该有它的存储值 由表达式求值最多修改一次。此外,先验值 应该是只读的,以确定要存储的值。

和6.5.16赋值操作符,§4:

操作数的求值顺序未指定。如果试图修改 赋值运算符的结果或在下一个序列点之后访问它的结果 行为是未定义的。

其他回答

C有未定义行为的概念,也就是说,一些语言结构在语法上是有效的,但你不能预测代码运行时的行为。

据我所知,该标准并没有明确说明为什么存在未定义行为的概念。在我看来,这只是因为语言设计者想在语义上有一些余地,而不是要求所有实现以完全相同的方式处理整数溢出,这很可能会带来严重的性能损失,他们只是让行为未定义,所以如果你编写的代码导致整数溢出,任何事情都可能发生。

那么,考虑到这一点,为什么这些是“问题”呢?语言清楚地表明,某些事情会导致未定义的行为。没有什么问题,没有什么“应该”牵涉其中。如果当其中一个涉及的变量被声明为volatile时,未定义的行为发生了变化,这并不能证明或改变任何东西。它是没有定义的;你不能对这种行为进行推理。

你最有趣的例子就是

u = (u++);

是教科书上未定义行为的例子(见维基百科关于序列点的条目)。

ISO W14站点的文档n1188提供了关于这种计算的一个很好的解释。

我解释这些想法。

适用于这种情况的ISO 9899标准的主要规则是6.5p2。

在前一个序列点和下一个序列点之间,通过表达式求值,对象的存储值最多修改一次。此外,前面的值只能被读取,以确定要存储的值。

像i=i++这样的表达式中的序列点在i=i之前和i++之后。

In the paper that I quoted above it is explained that you can figure out the program as being formed by small boxes, each box containing the instructions between 2 consecutive sequence points. The sequence points are defined in annex C of the standard, in the case of i=i++ there are 2 sequence points that delimit a full-expression. Such an expression is syntactically equivalent with an entry of expression-statement in the Backus-Naur form of the grammar (a grammar is provided in annex A of the Standard).

所以盒子里的指令顺序没有明确的顺序。

i=i++

可以解释为

tmp = i
i=i+1
i = tmp

或者是

tmp = i
i = tmp
i=i+1

因为解释代码i=i++的所有这些形式都是有效的,并且都生成不同的答案,所以行为是未定义的。

因此,序列点可以从组成程序的每个方框的开始和结束处看到(方框是C语言中的原子单位),并且在方框中,指令的顺序并不在所有情况下都是定义的。改变顺序有时会改变结果。

编辑:

其他解释这种歧义的很好的来源是c-faq网站(也出版了一本书)的条目,即这里、这里和这里。

虽然任何编译器和处理器实际上都不太可能这样做,但在C标准下,编译器使用以下序列实现"i++"是合法的:

In a single operation, read `i` and lock it to prevent access until further notice
Compute (1+read_value)
In a single operation, unlock `i` and store the computed value

虽然我不认为有任何处理器支持这样的硬件,可以有效地完成这样的事情,人们可以很容易地想象这样的行为会使多线程代码更容易(例如,它将保证如果两个线程同时尝试执行上面的序列,I将增加2),而且未来的某些处理器可能会提供类似的功能,这并不是完全不可想象的。

If the compiler were to write i++ as indicated above (legal under the standard) and were to intersperse the above instructions throughout the evaluation of the overall expression (also legal), and if it didn't happen to notice that one of the other instructions happened to access i, it would be possible (and legal) for the compiler to generate a sequence of instructions that would deadlock. To be sure, a compiler would almost certainly detect the problem in the case where the same variable i is used in both places, but if a routine accepts references to two pointers p and q, and uses (*p) and (*q) in the above expression (rather than using i twice) the compiler would not be required to recognize or avoid the deadlock that would occur if the same object's address were passed for both p and q.

C标准规定,一个变量最多只能在两个序列点之间赋值一次。例如,分号是一个序列点。 所以每个形式的表述

i = i++;
i = i++ + ++i;

等等都违反了这个规则。该标准还说行为是未定义的,而不是未指定的。一些编译器确实检测这些并产生一些结果,但这不是每个标准。

但是,两个不同的变量可以在两个序列点之间递增。

while(*src++ = *dst++);

以上是在复制/分析字符串时常见的编码实践。

这里的大多数答案都引用自C标准,强调这些构造的行为是未定义的。为了理解为什么这些结构的行为是未定义的,让我们先从C11标准的角度来理解这些术语:

测序(5.1.2.3):

给定任意两个评估A和B,如果A排在B之前,则A的执行应先于B的执行。

Unsequenced:

如果A不在B之前或之后测序,则A和B是未测序的。

评估可能是以下两种情况之一:

值计算,计算出表达式的结果;而且 副作用,也就是对对象的修改。

序列:

在表达式a和表达式B的求值之间存在序列点,意味着与a相关的每个值计算和副作用都在与B相关的每个值计算和副作用之前排序。

现在回到问题,对于像这样的表达

int i = 1;
i = i++;

标准说:

6.5表达式:

如果标量对象上的副作用相对于同一标量对象上的不同副作用或使用同一标量对象的值进行的值计算没有排序,则行为未定义。[…]

因此,上面的表达式调用UB,因为同一对象i上的两个副作用彼此之间没有顺序。这意味着赋值给i的副作用是在++的副作用之前还是之后,没有排序。 根据赋值是发生在增量之前还是之后,将产生不同的结果,这就是未定义行为的情况之一。

将赋值左边的i重命名为il,赋值右边(表达式i++)重命名为ir,则表达式为

il = ir++     // Note that suffix l and r are used for the sake of clarity.
              // Both il and ir represents the same object.  

关于postfix++操作符的重要一点是:

仅仅因为++出现在变量之后并不意味着增量发生得晚。只要编译器确保使用原始值,增量可以在编译器喜欢的时间发生。

这意味着表达式il = ir++可以被求值为

temp = ir;      // i = 1
ir = ir + 1;    // i = 2   side effect by ++ before assignment
il = temp;      // i = 1   result is 1  

or

temp = ir;      // i = 1
il = temp;      // i = 1   side effect by assignment before ++
ir = ir + 1;    // i = 2   result is 2  

导致两个不同的结果1和2,这取决于通过赋值和++的副作用的顺序,因此调用UB。