Why is the behavior different

Discussion in 'C++' started by ashish.dobhal@gmail.com, Apr 4, 2006.

  1. Guest

    i=0;
    printf("%d %d",i++,i);


    I am writing the above piece of code and executing it on different
    platforms (cygwin, solaris and linux). On cywin and linux the output is
    "0 0", whereas on solaris it is "0 1". What the language specification
    says about this case?


    Thanks,
    Ashish
     
    , Apr 4, 2006
    #1
    1. Advertising

  2. wrote:
    > i=0;
    > printf("%d %d",i++,i);
    >
    >
    > I am writing the above piece of code and executing it on different
    > platforms (cygwin, solaris and linux). On cywin and linux the output
    > is "0 0", whereas on solaris it is "0 1". What the language
    > specification says about this case?


    Afaik, it says its undefined, because you are reading and writing to
    the same variable before reaching a sequence point. Just do not write
    such code.

    rhth
    --
    jb

    (reply address in rot13, unscramble first)
     
    Jakob Bieling, Apr 4, 2006
    #2
    1. Advertising

  3. schrieb:
    > i=0;
    > printf("%d %d",i++,i);
    >
    >
    > I am writing the above piece of code and executing it on different
    > platforms (cygwin, solaris and linux). On cywin and linux the output is
    > "0 0", whereas on solaris it is "0 1". What the language specification
    > says about this case?


    5.2.2.8:

    The order of evaluation of arguments is unspecified. All side effects of argument expression evaluations
    take effect before the function is entered. The order of evaluation of the postfix expression and the argument
    expression list is unspecified.


    Stefan
    --
    Stefan Naewe
    naewe.s_AT_atlas_DOT_de
     
    =?ISO-8859-1?Q?Stefan_N=E4we?=, Apr 4, 2006
    #3
  4. Rolf Magnus Guest

    Stefan Näwe wrote:

    > schrieb:
    >> i=0;
    >> printf("%d %d",i++,i);
    >>
    >>
    >> I am writing the above piece of code and executing it on different
    >> platforms (cygwin, solaris and linux). On cywin and linux the output is
    >> "0 0", whereas on solaris it is "0 1". What the language specification
    >> says about this case?

    >
    > 5.2.2.8:
    >
    > The order of evaluation of arguments is unspecified. All side effects of
    > argument expression evaluations take effect before the function is
    > entered. The order of evaluation of the postfix expression and the
    > argument expression list is unspecified.


    However, this is not really significant here. The more important part is:

    5/4:

    Between the previous and next sequence point a scalar object shall have its
    stored value modified at most once by the evaluation of an expression.
    Furthermore, the prior value shall be accessed only to determine the value
    to be stored.
    The requirements of this paragraph shall be met for each allowable ordering
    of the subexpressions of a full expression; otherwise the behavior is
    undefined.


    Since i is used with the %d format specifier above, it must be an integer,
    for which operator++ doesn't give a sequence point, but it does modify i,
    which is read again for the second argument. In a function call, there is
    only a sequence point after _all_ arguments have been evaluated, not
    between them. So the behavior is not unspecified, but undefined.
     
    Rolf Magnus, Apr 4, 2006
    #4
    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. Johnny Ruin
    Replies:
    5
    Views:
    508
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    925
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,090
    Smokey Grindel
    Dec 2, 2006
  4. John Salerno
    Replies:
    7
    Views:
    277
    Terry Reedy
    Apr 24, 2008
  5. yuricake
    Replies:
    3
    Views:
    117
    Sylvain Joyeux
    May 5, 2007
Loading...

Share This Page