Need some explanation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rahul8143@gmail.com

    #1

    Need some explanation

    hello,
    1) First how following program get executed i mean how output is
    printed and also why following program gives different output in Turbo
    C++ compiler and Visual c++ 6 compiler?
    void main()
    {
    int val=5;
    printf("%d %d %d %d",val,--val,++val,val--);
    }
    under turbo compiler its giving
    4 4 5 5
    and under visual c++ its
    5 5 6 5

    2) How to evaluate following statement
    int val =5;
    val =- --val - val-- - --val;

  • Suman

    #2
    Re: Need some explanation



    rahul8143@gmail .com wrote:[color=blue]
    > hello,
    > 1) First how following program get executed i mean how output is
    > printed and also why following program gives different output in Turbo
    > C++ compiler and Visual c++ 6 compiler?[/color]
    #include <stdio.h>[color=blue]
    > void main()[/color]
    is not a portable standard definition for main, use:
    int main(/*int argc, char **argv*/)[color=blue]
    > {
    > int val=5;
    > printf("%d %d %d %d",val,--val,++val,val--);[/color]
    /*falling off main, is a rather recent addition C99 onwards, AFAIK,
    hence*/
    return 0;[color=blue]
    > }
    > under turbo compiler its giving
    > 4 4 5 5
    > and under visual c++ its
    > 5 5 6 5[/color]
    The order in which the arguments to printf are evaluated is *not* the
    same across different compiler/system pairs.[color=blue]
    > 2) How to evaluate following statement
    > int val =5;
    > val =- --val - val-- - --val;[/color]
    You might like to read the FAQ, and some stuff on side-effects and
    sequence points.

    Comment

    • Suman

      #3
      Re: Need some explanation



      rahul8143@gmail .com wrote:[color=blue]
      > hello,
      > 1) First how following program get executed i mean how output is
      > printed and also why following program gives different output in Turbo
      > C++ compiler and Visual c++ 6 compiler?
      > void main()
      > {
      > int val=5;
      > printf("%d %d %d %d",val,--val,++val,val--);
      > }
      > under turbo compiler its giving
      > 4 4 5 5
      > and under visual c++ its
      > 5 5 6 5
      >
      > 2) How to evaluate following statement
      > int val =5;
      > val =- --val - val-- - --val;[/color]

      Goto http://www.eskimo.com/~scs/C-faq/s3.html.

      Comment

      • akarl

        #4
        Re: Need some explanation

        rahul8143@gmail .com wrote:[color=blue]
        > hello,
        > 1) First how following program get executed i mean how output is
        > printed and also why following program gives different output in Turbo
        > C++ compiler and Visual c++ 6 compiler?
        > void main()
        > {
        > int val=5;
        > printf("%d %d %d %d",val,--val,++val,val--);
        > }
        > under turbo compiler its giving
        > 4 4 5 5
        > and under visual c++ its
        > 5 5 6 5
        >
        > 2) How to evaluate following statement
        > int val =5;
        > val =- --val - val-- - --val;[/color]

        In the first case the behavior is undefined (evaluation order of
        function parameters). The statement in (2) is very hard to read and
        should (in practice) be rewritten into something more comprehensible.

        Moreover:

        - `void main()' is not a valid ANSI C signature for the main function.

        - You have not included stdio.h

        - Your coding style is inconsistent and the indentation is strange to
        say the least.

        Comment

        • John Bode

          #5
          Re: Need some explanation



          rahul8143@gmail .com wrote:[color=blue]
          > hello,
          > 1) First how following program get executed i mean how output is
          > printed and also why following program gives different output in Turbo
          > C++ compiler and Visual c++ 6 compiler?[/color]

          It's because you are invoking undefined behavior in both cases. Read
          the following: http://www.eskimo.com/~scs/C-faq/s3.html

          The expression i++ evaluates to the current value of i; the side affect
          (i incremented by 1) is applied sometime before the next sequence
          point, but it need not be immediately after the expression is
          evaluated.

          Similarly for ++i; it evaluates to the current value of i + 1, but i is
          not necessarily incremented immediately.

          There is no requirement on when the side affects are applied other than
          it must happen before the next sequence point.

          For example, given the statement

          i = j++ + ++k;

          the following sequence is possible:

          t1 <- j
          t2 <- k + 1
          i <- t1 + t2
          j <- j + 1
          k <- k + 1

          Same thing applies to your printf() statement; there's no sequence
          point between the autoincrement/decrement expressions, so the behavior
          is undefined, meaning the compiler can do anything it wants.
          [color=blue]
          > void main()[/color]

          You mean int main(void). Strictly speaking, "void main()" is an error.
          [color=blue]
          > {
          > int val=5;
          > printf("%d %d %d %d",val,--val,++val,val--);
          > }
          > under turbo compiler its giving
          > 4 4 5 5
          > and under visual c++ its
          > 5 5 6 5
          >
          > 2) How to evaluate following statement
          > int val =5;
          > val =- --val - val-- - --val;[/color]

          Comment

          • Christian Kandeler

            #6
            Re: Need some explanation

            akarl wrote:
            [color=blue][color=green]
            >> 1) First how following program get executed i mean how output is
            >> printed and also why following program gives different output in Turbo
            >> C++ compiler and Visual c++ 6 compiler?
            >> void main()
            >> {
            >> int val=5;
            >> printf("%d %d %d %d",val,--val,++val,val--);
            >> }
            >> under turbo compiler its giving
            >> 4 4 5 5
            >> and under visual c++ its
            >> 5 5 6 5
            >>
            >> 2) How to evaluate following statement
            >> int val =5;
            >> val =- --val - val-- - --val;[/color]
            >
            > In the first case the behavior is undefined (evaluation order of
            > function parameters). The statement in (2) is very hard to read and
            > should (in practice) be rewritten into something more comprehensible.[/color]

            The second case is also undefined, for the same reason the first one is
            (repeated modifications of an object without an intervening sequence
            point).


            Christian

            Comment

            • CBFalconer

              #7
              Re: Need some explanation

              rahul8143@gmail .com wrote:[color=blue]
              >
              > 1) First how following program get executed i mean how output is
              > printed and also why following program gives different output in
              > Turbo C++ compiler and Visual c++ 6 compiler?
              > void main()
              > {
              > int val=5;
              > printf("%d %d %d %d",val,--val,++val,val--);
              > }
              > under turbo compiler its giving
              > 4 4 5 5
              > and under visual c++ its
              > 5 5 6 5
              >
              > 2) How to evaluate following statement
              > int val =5;
              > val =- --val - val-- - --val;[/color]

              Get your C book or the standard and read up on sequence points
              and/or parameter evaluation order. This is elementary. And if you
              really mean C++ this is not the place.

              --
              "If you want to post a followup via groups.google.c om, don't use
              the broken "Reply" link at the bottom of the article. Click on
              "show options" at the top of the article, then click on the
              "Reply" at the bottom of the article headers." - Keith Thompson


              Comment

              • akarl

                #8
                Re: Need some explanation

                John Bode wrote:[color=blue]
                >
                > rahul8143@gmail .com wrote:
                >[color=green]
                >>hello,
                >> 1) First how following program get executed i mean how output is
                >>printed and also why following program gives different output in Turbo
                >>C++ compiler and Visual c++ 6 compiler?[/color]
                >
                >
                > It's because you are invoking undefined behavior in both cases. Read
                > the following: http://www.eskimo.com/~scs/C-faq/s3.html
                >
                > The expression i++ evaluates to the current value of i; the side affect
                > (i incremented by 1) is applied sometime before the next sequence
                > point, but it need not be immediately after the expression is
                > evaluated.
                >
                > Similarly for ++i; it evaluates to the current value of i + 1, but i is
                > not necessarily incremented immediately.
                >
                > There is no requirement on when the side affects are applied other than
                > it must happen before the next sequence point.
                >
                > For example, given the statement
                >
                > i = j++ + ++k;
                >
                > the following sequence is possible:
                >
                > t1 <- j
                > t2 <- k + 1
                > i <- t1 + t2
                > j <- j + 1
                > k <- k + 1
                >
                > Same thing applies to your printf() statement; there's no sequence
                > point between the autoincrement/decrement expressions, so the behavior
                > is undefined, meaning the compiler can do anything it wants.
                >
                >[color=green]
                >>void main()[/color]
                >
                >
                > You mean int main(void). Strictly speaking, "void main()" is an error.
                >
                >[color=green]
                >> {
                >> int val=5;
                >> printf("%d %d %d %d",val,--val,++val,val--);
                >> }
                >>under turbo compiler its giving
                >>4 4 5 5
                >>and under visual c++ its
                >> 5 5 6 5
                >>
                >>2) How to evaluate following statement
                >>int val =5;
                >> val =- --val - val-- - --val;[/color][/color]

                How come statements like these compile when the result is undefined? Is
                it too hard for the compiler to figure out that it results in undefined
                behavior?

                Comment

                • Richard Bos

                  #9
                  Re: Need some explanation

                  akarl <fusionfive@com hem.se> wrote:
                  [color=blue]
                  > John Bode wrote:[color=green]
                  > >
                  > > rahul8143@gmail .com wrote:[color=darkred]
                  > >>2) How to evaluate following statement
                  > >>int val =5;
                  > >> val =- --val - val-- - --val;[/color][/color]
                  >
                  > How come statements like these compile when the result is undefined? Is
                  > it too hard for the compiler to figure out that it results in undefined
                  > behavior?[/color]

                  In a simple case like this, perhaps not; but in the general case, yes.

                  Richard

                  Comment

                  • John Bode

                    #10
                    Re: Need some explanation



                    akarl wrote:[color=blue]
                    > John Bode wrote:[color=green]
                    > >
                    > > rahul8143@gmail .com wrote:
                    > >[color=darkred]
                    > >>hello,
                    > >> 1) First how following program get executed i mean how output is
                    > >>printed and also why following program gives different output in Turbo
                    > >>C++ compiler and Visual c++ 6 compiler?[/color]
                    > >
                    > >
                    > > It's because you are invoking undefined behavior in both cases. Read
                    > > the following: http://www.eskimo.com/~scs/C-faq/s3.html
                    > >[/color][/color]

                    [snip]
                    [color=blue]
                    >
                    > How come statements like these compile when the result is undefined?[/color]

                    Well, "undefined behavior" basically means the compiler is free to
                    handle the problem in any way it wants to. As soon as the Standard
                    mandates that the compiler issue a diagnostic or abort, the behavior is
                    no longer undefined.
                    [color=blue]
                    > Is
                    > it too hard for the compiler to figure out that it results in undefined
                    > behavior?[/color]

                    I'm not a compiler writer by trade, so I can't say for sure. In the
                    obvious case (i = i++), it shouldn't be too hard. But imagine a case
                    like this:

                    foo.c
                    -----------------------
                    int foo(int *p, int *q)
                    {
                    return (*p)++ + ++(*q);
                    }

                    main.c
                    -----------------------
                    extern int foo(int *p, int *q);

                    int main(void)
                    {
                    int i, *j, *k;
                    int x;

                    k = &i;
                    i = 5;
                    j = k;

                    x = foo(j, k);
                    return 0;
                    }

                    Not only is i aliased by pointers, the foo() function is compiled
                    separately from the main() function. There's simply no way to detect
                    that i is being modified more than once between sequence points at
                    compile time.

                    Situations like that would be impossible to guard against. That's why
                    the Standard leaves the behavior "undefined" ; there's simply no good
                    way to catch all instances of this problem at compile time, so no
                    requirement is placed on the compiler implementor to do so.

                    Comment

                    • Chris Dollin

                      #11
                      Re: Need some explanation

                      akarl wrote:
                      [color=blue][color=green][color=darkred]
                      >>>2) How to evaluate following statement
                      >>>int val =5;
                      >>> val =- --val - val-- - --val;[/color][/color]
                      >
                      > How come statements like these compile when the result is undefined? Is
                      > it too hard for the compiler to figure out that it results in undefined
                      > behavior?[/color]

                      In general, yes. (I believe GCC spots some simple cases.)

                      Consider:

                      ... ++*bill + ++*ben ...

                      This is undefined if bill and ben point to the same location,
                      but well-defined [assuming no overflow ...] if they don't.

                      --
                      Chris "electric hedgehog" Dollin
                      It's called *extreme* programming, not *stupid* programming.

                      Comment

                      • Jirka Klaue

                        #12
                        Re: Need some explanation

                        John Bode:
                        ....[color=blue]
                        > Well, "undefined behavior" basically means the compiler is free to
                        > handle the problem in any way it wants to. As soon as the Standard
                        > mandates that the compiler issue a diagnostic or abort, the behavior is
                        > no longer undefined.[/color]

                        If the undefined behavior is also a constraint violation a diagnostic
                        must be issued. (3.4.3#2, 5.1.1.3)

                        Jirka

                        Comment

                        • Ben Pfaff

                          #13
                          Re: Need some explanation

                          akarl <fusionfive@com hem.se> writes:
                          [color=blue]
                          > [...for something like x[i++] = i...]
                          > How come statements like these compile when the result is undefined?
                          > Is it too hard for the compiler to figure out that it results in
                          > undefined behavior?[/color]

                          Sufficiently new versions of GCC will often warn about statements
                          like this.
                          --
                          Ben Pfaff
                          email: blp@cs.stanford .edu
                          web: http://benpfaff.org

                          Comment

                          • Flash Gordon

                            #14
                            Re: Need some explanation

                            akarl wrote:[color=blue]
                            > John Bode wrote:
                            >[color=green]
                            >> rahul8143@gmail .com wrote:[/color][/color]

                            <snip>
                            [color=blue][color=green][color=darkred]
                            >>> 2) How to evaluate following statement
                            >>> int val =5;
                            >>> val =- --val - val-- - --val;[/color][/color]
                            >
                            > How come statements like these compile when the result is undefined? Is
                            > it too hard for the compiler to figure out that it results in undefined
                            > behavior?[/color]

                            In these simple cases it is reasonably obvious there is undefined
                            behaviour. However, with
                            val = (*ptra)-- - --(*ptrb)
                            How can the compiler reliably determine whether there is undefined
                            behaviour or not?

                            So the standard does not require that compilers diagnose undefined
                            behaviour.

                            However, some compilers *will* diagnose *some* instances of undefined
                            behaviour if invoked with the correct switches. For example, the with
                            gcc -Wsequence-point catches a number of these cases, but it is not perfect.
                            --
                            Flash Gordon
                            Living in interesting times.
                            Although my email address says spam, it is real and I read it.

                            Comment

                            • CBFalconer

                              #15
                              Re: Need some explanation

                              akarl wrote:[color=blue]
                              >[/color]
                              .... snip ...[color=blue]
                              >
                              > How come statements like these compile when the result is
                              > undefined? Is it too hard for the compiler to figure out that
                              > it results in undefined behavior?[/color]

                              Why should it? It doesn't have to, because things are
                              syntactically correct. You are always allowed to use a safer
                              language, such as Pascal or Ada, if you wish. They have been
                              designed to have the necessary redundancy to detect such things. C
                              was designed to replace assembly language.

                              --
                              "If you want to post a followup via groups.google.c om, don't use
                              the broken "Reply" link at the bottom of the article. Click on
                              "show options" at the top of the article, then click on the
                              "Reply" at the bottom of the article headers." - Keith Thompson


                              Comment

                              Working...