首先评估表达式的哪一侧?
表达式的右侧将首先被评估还是左侧?
void main ()
{
int i = 0 , a[3] ;
a[i] = i++;
printf ("%d",a[i]) ;
}
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]) ;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
赋值运算符的操作数的求值顺序是未指定的:操作数可以按任何顺序求值。
但是,此表达式 (
a[i] = i++
) 会产生未定义的行为,因为您既修改了i
(使用i++
),又分别读取了 < code>i (使用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 modifyi
(usingi++
) and you separately readi
(usinga[i]
) without a sequence point in between those actions.C 没有定义首先评估哪一侧。标准规定 (C99 §6.5/2):
您发布的上述结果即为 UB。
C does not define which side gets evaluated first. The standard states (C99 §6.5/2):
The aforementioned result you posted is thereby UB.