Question regarding sequence point

Discussion in 'C Programming' started by somenath, Nov 26, 2007.

  1. somenath

    somenath Guest

    Hi All,
    I have one question regarding the code mentioned bellow.

    #include<stdio.h>
    int main(void)
    {
    unsigned int i = 5;
    printf("\n i = %d i<<2 = %d i>>2 =%d\n",i,i<<=2,i>>=2);
    return 0;

    }

    The output of the following code is as mentioned bellow.
    i = 4 i<<2 = 4 i>>2 =1

    I know the behavior of the code is undefined because
    1) Order of evaluation of function arguments are not defined by C
    standard.
    My question is
    Does the following code not trying to modify the value of "i " with
    in one sequence point ?

    Regards,
    Somenath
    somenath, Nov 26, 2007
    #1
    1. Advertising

  2. somenath

    Jack Klein Guest

    On Sun, 25 Nov 2007 18:47:04 -0800 (PST), somenath
    <> wrote in comp.lang.c:

    > Hi All,
    > I have one question regarding the code mentioned bellow.
    >
    > #include<stdio.h>
    > int main(void)
    > {
    > unsigned int i = 5;
    > printf("\n i = %d i<<2 = %d i>>2 =%d\n",i,i<<=2,i>>=2);
    > return 0;
    >
    > }
    >
    > The output of the following code is as mentioned bellow.
    > i = 4 i<<2 = 4 i>>2 =1
    >
    > I know the behavior of the code is undefined because
    > 1) Order of evaluation of function arguments are not defined by C
    > standard.


    Actually, that is both not true and does not make the behavior
    undefined. The order of evaluation of function arguments is
    "unspecified" by the C standard, which is a distinct type of behavior
    completely different than "undefined".

    Unspecified behavior means that the compiler (or more specifically,
    the programmers who wrote the compiler) must pick some order, but they
    do not have to document what it is, nor does it have to be consistent.

    Consider:

    #include <stdio.h>

    int val(void)
    {
    static int x = 3;
    return x++;
    }

    int main(void)
    {
    printf("%d %d\n", val(), val());
    return 0;
    }

    This code has "unspecified behavior", but not "undefined behavior".
    The compiler must generate code to call val() twice. The result of
    one of these calls is passed as the second argument to printf(), the
    result of the other call is passed as the third argument to printf().

    The output can be either:

    3 4

    ....or

    4 3

    ....and the compiler documentation does not have to specify which. In
    fact it may change from one to the other when the compiler is invoked
    with different options.

    There is no undefined behavior here, because each time the value of
    'x' is modified, it is inside its own function, which introduces a
    sequence point.

    And since there is no undefined behavior involved, the program must
    output one of these two sequences here, there are no other
    possibilities allowed.

    > My question is
    > Does the following code not trying to modify the value of "i " with
    > in one sequence point ?


    In fact, the one and only reason your code example has undefined
    behavior is because it violates two rules relating to sequence points,
    not just one.

    First, the value of 'i' is indeed modified twice without an
    intervening sequence point.

    Second, the value of 'i' is accessed other than in to determine its
    new value.

    > Regards,
    > Somenath


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Nov 26, 2007
    #2
    1. Advertising

  3. somenath

    somenath Guest

    On Nov 26, 8:08 am, Jack Klein <> wrote:
    > On Sun, 25 Nov 2007 18:47:04 -0800 (PST), somenath
    > <> wrote in comp.lang.c:
    >
    >
    >
    >
    >
    > > Hi All,
    > > I have one question regarding the code mentioned bellow.

    >
    > > #include<stdio.h>
    > > int main(void)
    > > {
    > > unsigned int i = 5;
    > > printf("\n i = %d i<<2 = %d i>>2 =%d\n",i,i<<=2,i>>=2);
    > > return 0;

    >
    > > }

    >
    > > The output of the following code is as mentioned bellow.
    > > i = 4 i<<2 = 4 i>>2 =1

    >
    > > I know the behavior of the code is undefined because
    > > 1) Order of evaluation of function arguments are not defined by C
    > > standard.

    >
    > Actually, that is both not true and does not make the behavior
    > undefined. The order of evaluation of function arguments is
    > "unspecified" by the C standard, which is a distinct type of behavior
    > completely different than "undefined".
    >

    Many thanks for your response. I would like to illustrate why I have
    said "undefined" .
    The while I compiled the code using gcc -Wall I got the following
    warnings

    gcc -Wall ub.c
    ub.c: In function `main':
    ub.c:5: warning: operation on `i' may be undefined
    ub.c:5: warning: operation on `i' may be undefined



    > Unspecified behavior means that the compiler (or more specifically,
    > the programmers who wrote the compiler) must pick some order, but they
    > do not have to document what it is, nor does it have to be consistent.
    >
    > Consider:
    >
    > #include <stdio.h>
    >
    > int val(void)
    > {
    > static int x = 3;
    > return x++;
    >
    > }
    >
    > int main(void)
    > {
    > printf("%d %d\n", val(), val());
    > return 0;
    >
    > }
    >
    > This code has "unspecified behavior", but not "undefined behavior".
    > The compiler must generate code to call val() twice. The result of
    > one of these calls is passed as the second argument to printf(), the
    > result of the other call is passed as the third argument to printf().
    >
    > The output can be either:
    >
    > 3 4
    >
    > ...or
    >
    > 4 3
    >
    > ...and the compiler documentation does not have to specify which. In
    > fact it may change from one to the other when the compiler is invoked
    > with different options.
    >
    > There is no undefined behavior here, because each time the value of
    > 'x' is modified, it is inside its own function, which introduces a
    > sequence point.
    >
    > And since there is no undefined behavior involved, the program must
    > output one of these two sequences here, there are no other
    > possibilities allowed.
    >
    > > My question is
    > > Does the following code not trying to modify the value of "i " with
    > > in one sequence point ?

    >
    > In fact, the one and only reason your code example has undefined
    > behavior is because it violates two rules relating to sequence points,
    > not just one.
    >
    > First, the value of 'i' is indeed modified twice without an
    > intervening sequence point.
    >
    > Second, the value of 'i' is accessed other than in to determine its
    > new value.
    somenath, Nov 26, 2007
    #3
  4. somenath

    CBFalconer Guest

    somenath wrote:
    > Jack Klein <> wrote:
    >

    .... snip ...
    >>
    >> Actually, that is both not true and does not make the behavior
    >> undefined. The order of evaluation of function arguments is
    >> "unspecified" by the C standard, which is a distinct type of
    >> behavior completely different than "undefined".

    >
    > Many thanks for your response. I would like to illustrate why I
    > have said "undefined" .
    > The while I compiled the code using gcc -Wall I got the following
    > warnings
    >
    > gcc -Wall ub.c
    > ub.c: In function `main':
    > ub.c:5: warning: operation on `i' may be undefined
    > ub.c:5: warning: operation on `i' may be undefined


    I have quoted your entire reply, together with what it responded
    to. Your actual reply consisted of an entirely useless 106 lines,
    whose main function was to hide your actual reply. Please quote
    and snip correctly. See the links in my sig. below.

    --
    Some useful links on quoting:
    <http://www.xs4all.nl/%7ewijnands/nnq/nquote.html>
    <http://www.complang.tuwien.ac.at/anton/mail-news-errors.html>
    <http://www.netmeister.org/news/learn2quote2.html>
    <http://www.star-one.org.uk/computer/format.htm>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Nov 26, 2007
    #4
  5. somenath

    santosh Guest

    In article
    <>,
    somenath <> wrote on Monday 26 Nov 2007 9:14 am:

    > On Nov 26, 8:08 am, Jack Klein <> wrote:
    >> On Sun, 25 Nov 2007 18:47:04 -0800 (PST), somenath
    >> <> wrote in comp.lang.c:
    >>
    >>
    >>
    >>
    >>
    >> > Hi All,
    >> > I have one question regarding the code mentioned bellow.

    >>
    >> > #include<stdio.h>
    >> > int main(void)
    >> > {
    >> > unsigned int i = 5;
    >> > printf("\n i = %d i<<2 = %d i>>2 =%d\n",i,i<<=2,i>>=2);
    >> > return 0;

    >>
    >> > }

    >>
    >> > The output of the following code is as mentioned bellow.
    >> > i = 4 i<<2 = 4 i>>2 =1

    >>
    >> > I know the behavior of the code is undefined because
    >> > 1) Order of evaluation of function arguments are not defined by C
    >> > standard.

    >>
    >> Actually, that is both not true and does not make the behavior
    >> undefined. The order of evaluation of function arguments is
    >> "unspecified" by the C standard, which is a distinct type of behavior
    >> completely different than "undefined".
    >>

    > Many thanks for your response. I would like to illustrate why I have
    > said "undefined" .
    > The while I compiled the code using gcc -Wall I got the following
    > warnings
    >
    > gcc -Wall ub.c
    > ub.c: In function `main':
    > ub.c:5: warning: operation on `i' may be undefined
    > ub.c:5: warning: operation on `i' may be undefined


    That's because your function call does invoke undefined behaviour, but
    not for the reason that you (seemingly) thought.

    <snip>

    PS. To get even more diagnostics and conformance add the '-W', '-ansi'
    and '-pedantic' options to the gcc invocation. Of course, to compile
    code using C99 features, use '-std=c99' instead of '-ansi'.
    santosh, Nov 26, 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. Gene

    Sequence point question

    Gene, Jun 23, 2007, in forum: C Programming
    Replies:
    5
    Views:
    270
  2. stef mientki
    Replies:
    13
    Views:
    616
    stef mientki
    Oct 20, 2007
  3. somenath
    Replies:
    4
    Views:
    290
  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