Velocity Reviews > Question regarding sequence point in case of conditional operator

# Question regarding sequence point in case of conditional operator

somenath
Guest
Posts: n/a

 12-14-2007

Hi All,

I have one question regarding the conditional operator.
In the draft C99 standard it is mentioned that

"1 The following are the sequence points described in 5.1.2.3:
-- The call to a function, after the arguments have been evaluated
(6.5.2.2).
-- The end of the first operand of the following operators: logical AND
&& (6.5.13);
logical OR || (6.5.14); conditional ? (6.5.15); comma , (6.5.17)."

My question is conditional operator consist of "?" and ":" . Now if
I try to modify the value of one particular variable between "?" and
": " will it show undefined behavior?
For example

b = (a++)? a++: a++;
Here is the second increment of a++ will be undefined?

Regards,
Somenath

Chris Dollin
Guest
Posts: n/a

 12-14-2007
somenath wrote:

> I have one question regarding the conditional operator.
> In the draft C99 standard it is mentioned that
>
> "1 The following are the sequence points described in 5.1.2.3:
> -- The call to a function, after the arguments have been evaluated
> (6.5.2.2).
> -- The end of the first operand of the following operators: logical AND
> && (6.5.13);
> logical OR || (6.5.14); conditional ? (6.5.15); comma , (6.5.17)."
>
> My question is conditional operator consist of "?" and ":"

Well, no, it has three expressions in there also: `test ? ifso : ifnot`.
There's a sequence point after the first operand, ie, `test`. The second
(or third) operand is [as if it is] evaluated once the test is completed.

> . Now if
> I try to modify the value of one particular variable between "?" and
> ": " will it show undefined behavior?
> For example
>
> b = (a++)? a++: a++;
> Here is the second increment of a++ will be undefined?

No. Why do you think it might be?

--
Chris "see Quen's point?" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

somenath
Guest
Posts: n/a

 12-14-2007
On Dec 14, 3:19 pm, Chris Dollin <(E-Mail Removed)> wrote:
> somenath wrote:
> > I have one question regarding the conditional operator.
> > In the draft C99 standard it is mentioned that

>
> > "1 The following are the sequence points described in 5.1.2.3:
> > -- The call to a function, after the arguments have been evaluated
> > (6.5.2.2).
> > -- The end of the first operand of the following operators: logical AND
> > && (6.5.13);
> > logical OR || (6.5.14); conditional ? (6.5.15); comma , (6.5.17)."

>
> > My question is conditional operator consist of "?" and ":"

>
> Well, no, it has three expressions in there also: `test ? ifso : ifnot`.
> There's a sequence point after the first operand, ie, `test`. The second
> (or third) operand is [as if it is] evaluated once the test is completed.
>
> > . Now if
> > I try to modify the value of one particular variable between "?" and
> > ": " will it show undefined behavior?
> > For example

>
> > b = (a++)? a++: a++;
> > Here is the second increment of a++ will be undefined?

>
> No. Why do you think it might be?

Sorry for posting the same question twice.
In the standard it specifically says about "conditional ? (6.5.15);"
So I had the doubt that is it true for ":" also ?

Joachim Schmitz
Guest
Posts: n/a

 12-14-2007
"somenath" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> On Dec 14, 3:19 pm, Chris Dollin <(E-Mail Removed)> wrote:
>> somenath wrote:
>> > I have one question regarding the conditional operator.
>> > In the draft C99 standard it is mentioned that

>>
>> > "1 The following are the sequence points described in 5.1.2.3:
>> > -- The call to a function, after the arguments have been evaluated
>> > (6.5.2.2).
>> > -- The end of the first operand of the following operators: logical AND
>> > && (6.5.13);
>> > logical OR || (6.5.14); conditional ? (6.5.15); comma , (6.5.17)."

>>
>> > My question is conditional operator consist of "?" and ":"

>>
>> Well, no, it has three expressions in there also: `test ? ifso : ifnot`.
>> There's a sequence point after the first operand, ie, `test`. The second
>> (or third) operand is [as if it is] evaluated once the test is completed.
>>
>> > . Now if
>> > I try to modify the value of one particular variable between "?" and
>> > ": " will it show undefined behavior?
>> > For example

>>
>> > b = (a++)? a++: a++;
>> > Here is the second increment of a++ will be undefined?

>>
>> No. Why do you think it might be?

>
> Sorry for posting the same question twice.
> In the standard it specifically says about "conditional ? (6.5.15);"
> So I had the doubt that is it true for ":" also ?

It seem irrelevant whether ":" is a sequence point, it does separate the
"if" from the "else" branch, so only one of the statements get
executed/evaluated anyway. Then finally the ; gets hit, which again is a
sequence point.

Bye, Jojo

pete
Guest
Posts: n/a

 12-14-2007
somenath wrote:

> > > In the draft C99 standard it is mentioned that

> In the standard it specifically says about "conditional ? (6.5.15);"
> So I had the doubt that is it true for ":" also ?

That's a typographical error.
The conditional operator has two parts, ? and :
like parentheses do, ( and )

This is the conditional operator as shown in the N869 index:
conditional operator (? , 6.5.15

In other parts of the draft, it's also written as ?:

6.5 Expressions

[#3] The grouping of operators and operands is indicated by
the syntax.61) Except as specified later (for the function-
call (), &&, ||, ?:, and comma operators), the order of
evaluation of subexpressions and the order in which side
effects take place are both unspecified.

61)
The exceptions are cast expressions (6.5.4) as operands
of unary operators (6.5.3), and an operand contained
between any of the following pairs of operators: grouping
parentheses () (6.5.1), subscripting brackets []
(6.5.2.1), function-call parentheses () (6.5.2.2), and
the conditional operator ?: (6.5.15).

--
pete