简体   繁体   English

首先评估表达式的哪一侧?

[英]which side of the expression gets evaluated first?

Will the right side of the expression get evaluated first or the left ? 表达式的右侧是先评估还是左侧评估?

void main ()
{
    int i = 0 , a[3] ;
    a[i] = i++;
    printf ("%d",a[i]) ;
}

The order of evaluation of the operands of the assignment operator is unspecified: the operands may be evaluated in any order. 未指定赋值运算符的操作数的评估顺序:可以按任何顺序评估操作数。

However, this expression ( a[i] = i++ ) yields undefined behavior because you both modify i (using i++ ) and you separately read i (using a[i] ) without a sequence point in between those actions. 但是,这个表达式( a[i] = i++ )会产生未定义的行为,因为你们都修改了i (使用i++ )而你们分别读取i (使用a[i] )而没有这些动作之间的序列点。

C does not define which side gets evaluated first. C没有定义首先评估哪一方。 The standard states (C99 §6.5/2): 标准状态(C99§6.5/ 2):

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. 在前一个和下一个序列点之间,对象的存储值最多只能通过表达式的计算修改一次。 Furthermore, the prior value shall be accessed only to determine the value to be stored 此外,只能访问先前值以确定要存储的值

The aforementioned result you posted is thereby UB. 您发布的上述结果因此是UB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM