unexpected result

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ashu

    #1

    unexpected result

    hi
    look at this code

    include <stdio.h>
    int main(void)
    {
    int i,j=2;
    i=j++ * ++j * j++;
    printf("%d %d",i,j);
    return 0;
    }

    acc. to me the values of i & j are 27,5 respectively & rightly so as i


    ran this on turbo c++ compiler but if i ran this on lcc-win32 compiler


    i got 32 & 5 for i & j respectively.
    why this is so

  • Ankur

    #2
    Re: unexpected result

    hi anshu
    it all compiler depenedent
    in lcc-win32 compiler
    first the code run on right to left
    means i=4*4*2
    it give 32 for i
    but in turboc compiler it execute code left to right and take big value
    among it i.e. 3
    so thats y its give 27.


    bye
    Ankur

    ashu wrote:[color=blue]
    > hi
    > look at this code
    >
    > include <stdio.h>
    > int main(void)
    > {
    > int i,j=2;
    > i=j++ * ++j * j++;
    > printf("%d %d",i,j);
    > return 0;
    > }
    >
    > acc. to me the values of i & j are 27,5 respectively & rightly so as i
    >
    >
    > ran this on turbo c++ compiler but if i ran this on lcc-win32 compiler
    >
    >
    > i got 32 & 5 for i & j respectively.
    > why this is so[/color]

    Comment

    • Chris Dollin

      #3
      Re: unexpected result

      ashu wrote:
      [color=blue]
      > hi
      > look at this code
      >
      > include <stdio.h>
      > int main(void)
      > {
      > int i,j=2;
      > i=j++ * ++j * j++;[/color]

      Undefined behaviour: see the FAQ.

      (Informally speaking, you update j three times in random order;
      why do you expect the result to make sense? Except it's worse
      than that.)

      --
      Chris "sparqling" Dollin
      "Who do you serve, and who do you trust?"

      Comment

      • novice

        #4
        C and memory help.

        Hi geeks,
        Please tell me which book gives the complete
        detail of dynamic memory management in C and C++.

        Comment

        • Grumble

          #5
          Re: unexpected result

          ashu wrote:[color=blue]
          > hi
          > look at this code
          >
          > include <stdio.h>
          > int main(void)
          > {
          > int i,j=2;
          > i=j++ * ++j * j++;
          > printf("%d %d",i,j);
          > return 0;
          > }
          >
          > acc. to me the values of i & j are 27,5 respectively & rightly so as
          > i ran this on turbo c++ compiler but if i ran this on lcc-win32
          > compiler i got 32 & 5 for i & j respectively. why this is so[/color]

          This is a frequently asked question.

          Please read http://c-faq.com/expr/

          Comment

          • Keith Thompson

            #6
            Re: unexpected result

            "ashu" <riskyashish@ya hoo.com> writes:[color=blue]
            > include <stdio.h>[/color]

            Copy-and-paste carefully; you missed the '#'.
            [color=blue]
            > int main(void)
            > {
            > int i,j=2;
            > i=j++ * ++j * j++;
            > printf("%d %d",i,j);
            > return 0;
            > }[/color]

            Undefined behavior. Read section 3 of the comp.lang.c FAQ,
            <http://www.c-faq.com/>.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            • Joe Estock

              #7
              Re: C and memory help.

              novice wrote:[color=blue]
              > Hi geeks,
              > Please tell me which book gives the complete
              > detail of dynamic memory management in C and C++.
              >[/color]
              This has nothing to do with OP's question and should have been posted as
              a new thread; not a reply to an existing one.

              Google is a great place to find the information you are looking for
              since I'm not sure what you mean by "dynamic memory management in c". As
              far as C++ goes, you want comp.lang.c++ which is down the all first door
              on the right. If you have a question which is specific to *c* then by
              all means ask here (by posting in a new thread of course) and I'm sure
              someone here would be happy to help you.

              Joe

              Comment

              • srikanth

                #8
                Re: unexpected result

                i dont my version of compiler but the result on my system was 27 &5
                respectively

                Comment

                • Default User

                  #9
                  Re: unexpected result

                  srikanth wrote:
                  [color=blue]
                  > i dont my version of compiler but the result on my system was 27 &5
                  > respectively[/color]

                  Result of what? See below.



                  Brian

                  --
                  Please quote enough of the previous message for context. To do so from
                  Google, click "show options" and use the Reply shown in the expanded
                  header.

                  Comment

                  • John Tsiombikas (Nuclear / Mindlapse)

                    #10
                    Re: unexpected result

                    On 2006-03-08, srikanth <adsrikanth@gma il.com> wrote:[color=blue]
                    >
                    > i dont my version of compiler but the result on my system was 27 &5
                    > respectively
                    >[/color]

                    Please include context, by properly quoting the relevant parts of the
                    post you are replying to, so that people will understand what you are
                    talking about.

                    --
                    John Tsiombikas (Nuclear / Mindlapse)
                    nuclear@siggrap h.org

                    Comment

                    • John Tsiombikas (Nuclear / Mindlapse)

                      #11
                      Re: unexpected result

                      On 2006-03-08, ashu <riskyashish@ya hoo.com> wrote:[color=blue]
                      >
                      > hi
                      > look at this code
                      >
                      > include <stdio.h>
                      > int main(void)
                      > {
                      > int i,j=2;
                      > i=j++ * ++j * j++;[/color]

                      Undefined behaviour. You cannot modify a variable (j) more than once in
                      a single statement.
                      [color=blue]
                      > acc. to me the values of i & j are 27,5 respectively & rightly so as
                      > i ran this on turbo c++ compiler but if i ran this on lcc-win32
                      > compiler i got 32 & 5 for i & j respectively.[/color]

                      .... so the compiler is free to do anything with it, hence the variance.

                      --
                      John Tsiombikas (Nuclear / Mindlapse)
                      nuclear@siggrap h.org

                      Comment

                      • Jack Klein

                        #12
                        Re: unexpected result

                        On 8 Mar 2006 01:17:27 -0800, "Ankur" <ankur.mcp@gmai l.com> wrote in
                        comp.lang.c:

                        #1, DON'T TOP POST IN THIS GROUP. I have put your reply where it
                        belongs, after the original post.
                        [color=blue]
                        > ashu wrote:[color=green]
                        > > hi
                        > > look at this code
                        > >
                        > > include <stdio.h>
                        > > int main(void)
                        > > {
                        > > int i,j=2;
                        > > i=j++ * ++j * j++;
                        > > printf("%d %d",i,j);
                        > > return 0;
                        > > }
                        > >
                        > > acc. to me the values of i & j are 27,5 respectively & rightly so as i
                        > >
                        > >
                        > > ran this on turbo c++ compiler but if i ran this on lcc-win32 compiler
                        > >
                        > >
                        > > i got 32 & 5 for i & j respectively.
                        > > why this is so[/color]
                        >
                        > hi anshu
                        > it all compiler depenedent
                        > in lcc-win32 compiler
                        > first the code run on right to left
                        > means i=4*4*2
                        > it give 32 for i
                        > but in turboc compiler it execute code left to right and take big value
                        > among it i.e. 3
                        > so thats y its give 27.[/color]

                        No, it is not "all compiler dependent", it is undefined behavior.
                        There is no right result, there is no wrong result, as far as the C
                        language is concerned there is no need for a result at all.

                        --
                        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.l earn.c-c++

                        Comment

                        • jaysome

                          #13
                          Re: unexpected result

                          Jack Klein wrote:
                          [color=blue]
                          > On 8 Mar 2006 01:17:27 -0800, "Ankur" <ankur.mcp@gmai l.com> wrote in
                          > comp.lang.c:
                          >
                          > #1, DON'T TOP POST IN THIS GROUP. I have put your reply where it
                          > belongs, after the original post.
                          >
                          >[color=green]
                          >>ashu wrote:
                          >>[color=darkred]
                          >>>hi
                          >>>look at this code
                          >>>
                          >>>include <stdio.h>
                          >>>int main(void)
                          >>>{
                          >>> int i,j=2;
                          >>> i=j++ * ++j * j++;
                          >>> printf("%d %d",i,j);
                          >>> return 0;
                          >>>}
                          >>>
                          >>>acc. to me the values of i & j are 27,5 respectively & rightly so as i
                          >>>
                          >>>
                          >>> ran this on turbo c++ compiler but if i ran this on lcc-win32 compiler
                          >>>
                          >>>
                          >>>i got 32 & 5 for i & j respectively.
                          >>>why this is so[/color]
                          >>
                          >>hi anshu
                          >>it all compiler depenedent
                          >>in lcc-win32 compiler
                          >>first the code run on right to left
                          >>means i=4*4*2
                          >>it give 32 for i
                          >>but in turboc compiler it execute code left to right and take big value
                          >>among it i.e. 3
                          >>so thats y its give 27.[/color]
                          >
                          >
                          > No, it is not "all compiler dependent", it is undefined behavior.
                          > There is no right result, there is no wrong result, as far as the C
                          > language is concerned there is no need for a result at all.[/color]

                          There's always a result. But the result may not be as expected. Hence
                          unexpected behavior.

                          --
                          jay

                          Comment

                          • Vladimir S. Oka

                            #14
                            Re: unexpected result

                            On Friday 10 March 2006 06:20, jaysome opined (in
                            <AU8Qf.876$Dp5. 697@fe05.lga>):
                            [color=blue]
                            > Jack Klein wrote:
                            >[color=green]
                            >> On 8 Mar 2006 01:17:27 -0800, "Ankur" <ankur.mcp@gmai l.com> wrote in
                            >> comp.lang.c:
                            >>
                            >> #1, DON'T TOP POST IN THIS GROUP. I have put your reply where it
                            >> belongs, after the original post.
                            >>
                            >>[color=darkred]
                            >>>ashu wrote:
                            >>>
                            >>>>hi
                            >>>>look at this code
                            >>>>
                            >>>>include <stdio.h>
                            >>>>int main(void)
                            >>>>{
                            >>>>int i,j=2;
                            >>>>i=j++ * ++j * j++;
                            >>>>printf("% d %d",i,j);
                            >>>>return 0;
                            >>>>}
                            >>>>
                            >>>>acc. to me the values of i & j are 27,5 respectively & rightly so
                            >>>>as i
                            >>>>
                            >>>>
                            >>>> ran this on turbo c++ compiler but if i ran this on lcc-win32
                            >>>> compiler
                            >>>>
                            >>>>
                            >>>>i got 32 & 5 for i & j respectively.
                            >>>>why this is so
                            >>>
                            >>>hi anshu
                            >>>it all compiler depenedent
                            >>>in lcc-win32 compiler
                            >>>first the code run on right to left
                            >>>means i=4*4*2
                            >>>it give 32 for i
                            >>>but in turboc compiler it execute code left to right and take big
                            >>>value among it i.e. 3
                            >>>so thats y its give 27.[/color]
                            >>
                            >>
                            >> No, it is not "all compiler dependent", it is undefined behavior.
                            >> There is no right result, there is no wrong result, as far as the C
                            >> language is concerned there is no need for a result at all.[/color]
                            >
                            > There's always a result. But the result may not be as expected. Hence
                            > unexpected behavior.[/color]

                            Not "unexpected ": undefined. And, according to the C Standard, that
                            allows an implementation to do absolutely anything, including
                            absolutely nothing at all...

                            Unless, you count not producing any tangible result as a result in
                            itself, in which case we are in agreement about the effects.

                            --
                            BR, Vladimir

                            The best things in life are for a fee.

                            Comment

                            • Keith Thompson

                              #15
                              Re: unexpected result

                              jaysome <jaysome@spamco p.com> writes:[color=blue]
                              > Jack Klein wrote:[/color]
                              [...][color=blue][color=green][color=darkred]
                              >>>ashu wrote:[/color][/color][/color]
                              [...][color=blue][color=green][color=darkred]
                              >>>>include <stdio.h>
                              >>>>int main(void)
                              >>>>{
                              >>>> int i,j=2;
                              >>>> i=j++ * ++j * j++;
                              >>>> printf("%d %d",i,j);
                              >>>> return 0;
                              >>>>}[/color][/color][/color]
                              [...][color=blue][color=green]
                              >> No, it is not "all compiler dependent", it is undefined behavior.
                              >> There is no right result, there is no wrong result, as far as the C
                              >> language is concerned there is no need for a result at all.[/color]
                              >
                              > There's always a result. But the result may not be as expected. Hence
                              > unexpected behavior.[/color]

                              No, there is not always a result. One of the infinitely many possible
                              consequences of undefined behavior is that the code might not compile
                              in the first place. Other possibilities are that the program could do
                              nothing at all, or format your hard drive, or melt your monitor, or
                              make demons fly out of your nose.

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                              We must do something. This is something. Therefore, we must do this.

                              Comment

                              Working...