用C三元运营商和顺序点 [英] Ternary operator and Sequence Points in C

查看:117
本文介绍了用C三元运营商和顺序点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下形式的前pression如下图所示: -

I've an expression of the form shown below :-

while (count)
{
...
...

    index = ((count == 20)? 0 : index++);
...
...
}

现在三元运营商都在C序列点,但我相信,序列点测试部分结束。

Now Ternary operators are sequence points in C but I believe that the sequence point ends at the test part.

这是正确的认识,并因此将这一声明导致不确定的行为?

Is this understanding correct and as such will this statement lead to undefined behaviour ?

推荐答案

右键。还有的条件评估后的序列点,但是下一个序列点是分号终止的声明。所以每当算!= 20 ,你有不确定的行为

Right. There's a sequence point after the evaluation of the condition, but the next sequence point is the semicolon terminating the statement. So whenever count != 20, you have the undefined behaviour

index = index++;

因为首页没有干预序列点修改了两次。

since index is modified twice without intervening sequence point.

这篇关于用C三元运营商和顺序点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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