Question regarding sequence point in case of conditional operator

S

somenath

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
 
C

Chris Dollin

somenath said:
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?
 
S

somenath

somenath said:
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 ?
 
J

Joachim Schmitz

somenath said:
somenath said:
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
 
P

pete

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top