简体   繁体   English

三元运算符和C中的序列点

[英]Ternary operator and Sequence Points in C

I've an expression of the form shown below :- 我的表达式如下所示: -

while (count)
{
...
...

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

Now Ternary operators are sequence points in C but I believe that the sequence point ends at the test part. 现在三元运算符是C中的序列点,但我相信序列点在测试部分结束。

Is this understanding correct and as such will this statement lead to undefined behaviour ? 这种理解是否正确,因此这种说法会导致未定义的行为?

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 因此,只要count != 20 ,就会出现未定义的行为

index = index++;

since index is modified twice without intervening sequence point. 因为index被修改两次而没有插入序列点。

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

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