Question regarding sequence point in case of conditional operator

Discussion in 'C Programming' started by somenath, Dec 14, 2007.

  1. somenath

    somenath Guest

    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
    somenath, Dec 14, 2007
    #1
    1. Advertising

  2. somenath

    Chris Dollin Guest

    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
    Chris Dollin, Dec 14, 2007
    #2
    1. Advertising

  3. somenath

    somenath Guest

    On Dec 14, 3:19 pm, Chris Dollin <> 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 ?
    somenath, Dec 14, 2007
    #3
  4. "somenath" <> schrieb im Newsbeitrag
    news:...
    > On Dec 14, 3:19 pm, Chris Dollin <> 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
    Joachim Schmitz, Dec 14, 2007
    #4
  5. somenath

    pete Guest

    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
    pete, Dec 14, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alec S.
    Replies:
    10
    Views:
    10,080
    Alec S.
    Apr 16, 2005
  2. Gene

    Sequence point question

    Gene, Jun 23, 2007, in forum: C Programming
    Replies:
    5
    Views:
    270
  3. somenath

    Question regarding sequence point

    somenath, Nov 26, 2007, in forum: C Programming
    Replies:
    4
    Views:
    285
    santosh
    Nov 26, 2007
  4. somenath
    Replies:
    0
    Views:
    258
    somenath
    Dec 14, 2007
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,284
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page