有没有在EX pression序列分^ = B ^ = a ^ = B,或者是不确定的? [英] Are there sequence points in the expression a^=b^=a^=b, or is it undefined?

查看:114
本文介绍了有没有在EX pression序列分^ = B ^ = a ^ = B,或者是不确定的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

交换两个整型变量,而不是使用临时存储的所谓的聪明(但实际上效率低下)的方式,往往涉及这一行:

  int类型的= 10;
INT B = 42;一个^ = B ^ = a ^ = B; /*这里*/输出(A =%D,B =%d个\\ N,A,B);

但我想知道,复合赋值操作符,如 ^ = 不是序列点,是吗?
这是否意味着它实际上是不确定的行为?


解决方案

  A ^ = B ^ = a ^ = B; /*这里*/

这是不确定的行为。

您正在修改一个对象( A )超过两个序列点之间的一次。


  

(C99,6.5p2)的previous和下一个序列点之间的对象应由前pression的评价已经修改最多的一次存储的值。


简单的任务,以及复合分配不引入序列点。这里有恩pression声明前pression之前和前pression语句后的顺序点。

序列点是在C99的附录C(信息)上市和C11标准。

The allegedly "clever" (but actually inefficient) way of swapping two integer variables, instead of using temporary storage, often involves this line:

int a = 10;
int b = 42;

a ^= b ^= a ^= b; /*Here*/

printf("a=%d, b=%d\n", a, b); 

But I'm wondering, compound assignment operators like ^= are not sequence points, are they? Does this mean it's actually undefined behavior?

解决方案

a ^= b ^= a ^= b; /*Here*/

It is undefined behavior.

You are modifying an object (a) more than once between two sequence points.

(C99, 6.5p2) "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.

Simple assignments as well as compound assignments don't introduce a sequence point. Here there is a sequence point before the expression statement expression and after the expression statement.

Sequence points are listed in Annex C (informative) of the c99 and c11 Standard.

这篇关于有没有在EX pression序列分^ = B ^ = a ^ = B,或者是不确定的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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