Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. visual c++ funny or its a bug?

visual c++ funny or its a bug?

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharpjavahelpquestion
15 Posts 10 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    Now ppl this is funny! int var=10; cout<< (var++ * var++) ; it should print 110 but it actually doesn't. it prints 100 ;) but it works correctly (prints 110) on c#, java compilers and other c++ compilers.

    M Offline
    M Offline
    Maxwell Chen
    wrote on last edited by
    #3

    With VC++ 6.0, the result here is 100. (One hundred.) And I think "One hundred" is a correct result. Because when such an expression: int var = 10; cout << (var++ * var++); is equal to: int var = 10; cout << (var * var); var = var + 1; var = var + 1; // var = 12 now. Maxwell Chen

    D A 2 Replies Last reply
    0
    • M Maxwell Chen

      With VC++ 6.0, the result here is 100. (One hundred.) And I think "One hundred" is a correct result. Because when such an expression: int var = 10; cout << (var++ * var++); is equal to: int var = 10; cout << (var * var); var = var + 1; var = var + 1; // var = 12 now. Maxwell Chen

      D Offline
      D Offline
      DimpleSurana
      wrote on last edited by
      #4

      Yes even i do get 100 Cos v++ is equivalent to a post increment so the answer should be v*v & the value of v is then updated to 12

      1 Reply Last reply
      0
      • M Maxwell Chen

        With VC++ 6.0, the result here is 100. (One hundred.) And I think "One hundred" is a correct result. Because when such an expression: int var = 10; cout << (var++ * var++); is equal to: int var = 10; cout << (var * var); var = var + 1; var = var + 1; // var = 12 now. Maxwell Chen

        A Offline
        A Offline
        Abin
        wrote on last edited by
        #5

        No I don't think so, the results should be 110 because unary operators has higher precedence over binary operators, the correct steps will be as follwing: At the beginning var is 10. 1, calc the first "var++", result is 10 and var becomes 11. 2, calc the second "var++", result is 11 and var becomes 12. 3, calc the product of the first result(10) and the second result(11), which is 110. At the end var is 12.

        A S M 3 Replies Last reply
        0
        • A Abin

          No I don't think so, the results should be 110 because unary operators has higher precedence over binary operators, the correct steps will be as follwing: At the beginning var is 10. 1, calc the first "var++", result is 10 and var becomes 11. 2, calc the second "var++", result is 11 and var becomes 12. 3, calc the product of the first result(10) and the second result(11), which is 110. At the end var is 12.

          S Offline
          S Offline
          Sebastian Schneider
          wrote on last edited by
          #6

          Maxwell Chen is right. The result should be 100. Post-Increment means "Increment this variable after the operation the post-increment is used in". So a = b++ means "A = B. Increment B." and a= b++ * c++ means "A = B multiplied with C. Increment B. Increment C." Cheers Sebastian

          1 Reply Last reply
          0
          • A Abin

            No I don't think so, the results should be 110 because unary operators has higher precedence over binary operators, the correct steps will be as follwing: At the beginning var is 10. 1, calc the first "var++", result is 10 and var becomes 11. 2, calc the second "var++", result is 11 and var becomes 12. 3, calc the product of the first result(10) and the second result(11), which is 110. At the end var is 12.

            A Offline
            A Offline
            Alex H 1983
            wrote on last edited by
            #7

            u know * is Preeminent than ++ so at first RESULT will set to (Var * Var). but var++ will not change the result so result = (10*10) = 100 and val will updated to 12

            1 Reply Last reply
            0
            • A Abin

              No I don't think so, the results should be 110 because unary operators has higher precedence over binary operators, the correct steps will be as follwing: At the beginning var is 10. 1, calc the first "var++", result is 10 and var becomes 11. 2, calc the second "var++", result is 11 and var becomes 12. 3, calc the product of the first result(10) and the second result(11), which is 110. At the end var is 12.

              M Offline
              M Offline
              Maxwell Chen
              wrote on last edited by
              #8

              You might be confused... ;) Regarding to the example: cout << (var++ * var++); The meaning of "precedence" is, to take the expression as: cout << ((var++) * (var++)); // Means that parsing the postfix ++ token first. // Not means that counting the result of postfix ++ operation. rather than cout << ((var++) (*var)++); // Wrong. since operator * (Multiplicative) has lower precedence than operator ++ (postfix). Maxwell Chen

              A 1 Reply Last reply
              0
              • M Maxwell Chen

                You might be confused... ;) Regarding to the example: cout << (var++ * var++); The meaning of "precedence" is, to take the expression as: cout << ((var++) * (var++)); // Means that parsing the postfix ++ token first. // Not means that counting the result of postfix ++ operation. rather than cout << ((var++) (*var)++); // Wrong. since operator * (Multiplicative) has lower precedence than operator ++ (postfix). Maxwell Chen

                A Offline
                A Offline
                Abin
                wrote on last edited by
                #9

                Ok I see. You are right, sorry about the confusion.

                M 1 Reply Last reply
                0
                • A Abin

                  Ok I see. You are right, sorry about the confusion.

                  M Offline
                  M Offline
                  Maxwell Chen
                  wrote on last edited by
                  #10

                  :-D Maxwell Chen

                  1 Reply Last reply
                  0
                  • L Lost User

                    Now ppl this is funny! int var=10; cout<< (var++ * var++) ; it should print 110 but it actually doesn't. it prints 100 ;) but it works correctly (prints 110) on c#, java compilers and other c++ compilers.

                    M Offline
                    M Offline
                    Mike Dimmick
                    wrote on last edited by
                    #11

                    C++ only guarantees that all side-effects are complete by the point that a sequence point is reached. All function arguments must be evaluated before a function is called, but order of evaluation of the function arguments is implementation-defined. Visual C++ clearly evaluates var once, then performs the multiplication, then the post-increments. This looseness allows the compiler to produce more optimal code in some circumstances. C# and Java have strict left-to-right evaluation, so your code does work as you are expecting with those languages.

                    A 1 Reply Last reply
                    0
                    • L Lost User

                      Now ppl this is funny! int var=10; cout<< (var++ * var++) ; it should print 110 but it actually doesn't. it prints 100 ;) but it works correctly (prints 110) on c#, java compilers and other c++ compilers.

                      J Offline
                      J Offline
                      jbarton
                      wrote on last edited by
                      #12

                      Modifying var more than once between sequence points results in undefined behavior in C++. A C++ compiler can generate whatever results it wants from this. You could get 100, 110, 121, or any other value. Any result produced can't be considered correct or wrong - it is undefined.

                      1 Reply Last reply
                      0
                      • L Lost User

                        Now ppl this is funny! int var=10; cout<< (var++ * var++) ; it should print 110 but it actually doesn't. it prints 100 ;) but it works correctly (prints 110) on c#, java compilers and other c++ compilers.

                        W Offline
                        W Offline
                        warlu
                        wrote on last edited by
                        #13

                        if the program is: int var = 10; cout<<(var++ * var++); the result is 102 :omg: and if the program is like this: int var = 10; cout<<((var++)*(var++)); the result is 100; what i think is first the mutiplication operation is being taken place and the varible is getting incremented. in vc++ programming guide present in MSDN library unary operator (post increment) has the highest precedence ok u know the problem y it is happening pls send a mail

                        1 Reply Last reply
                        0
                        • M Mike Dimmick

                          C++ only guarantees that all side-effects are complete by the point that a sequence point is reached. All function arguments must be evaluated before a function is called, but order of evaluation of the function arguments is implementation-defined. Visual C++ clearly evaluates var once, then performs the multiplication, then the post-increments. This looseness allows the compiler to produce more optimal code in some circumstances. C# and Java have strict left-to-right evaluation, so your code does work as you are expecting with those languages.

                          A Offline
                          A Offline
                          abdul rafay abbasi
                          wrote on last edited by
                          #14

                          nicely explained but i am still not completely satisfied :(. just because when i implement my own class "myint" which has the post increment, the multiplication and the assignment operators overloaded and i use them as: myint a(10); myint c=(a++ * a++); i get value of c as 110 but when i use int i.e: int a=10; int c=(a++ * a++); i get c as 100 why this??? secondly in the case of cout<<(a++ * a++) u can say that (a++ * a++) comes under the case of "evaluation of function order" (though i don't agree) but when we use int c=(a++ * a++) ; it does not come under "evaluation of fuction order" it comes under "operator precedence" case, and it seems as if vc++ is voilating some rules. for ur information the "evaluation of function order" case is as: int a=1; cout<

                          M 1 Reply Last reply
                          0
                          • A abdul rafay abbasi

                            nicely explained but i am still not completely satisfied :(. just because when i implement my own class "myint" which has the post increment, the multiplication and the assignment operators overloaded and i use them as: myint a(10); myint c=(a++ * a++); i get value of c as 110 but when i use int i.e: int a=10; int c=(a++ * a++); i get c as 100 why this??? secondly in the case of cout<<(a++ * a++) u can say that (a++ * a++) comes under the case of "evaluation of function order" (though i don't agree) but when we use int c=(a++ * a++) ; it does not come under "evaluation of fuction order" it comes under "operator precedence" case, and it seems as if vc++ is voilating some rules. for ur information the "evaluation of function order" case is as: int a=1; cout<

                            M Offline
                            M Offline
                            Mike Dimmick
                            wrote on last edited by
                            #15

                            The C++ standard (ISO 14882:1998) at section 5, paragraph 5 reads: Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. 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. Example:

                            i = v[i++]; // the behavior is unspecified
                            i = 7, i++, i++; // i becomes 9

                            i = ++i + 1; // the behavior is unspecified
                            i = i + 1; // the value of i is incremented

                            All that C++ guarantees is that the side-effects of evaluating an expression are complete by the next sequence point - it does not specify an order of evaluation of terms in an expression, except where operator precedence is involved. The second example above works because the comma operator introduces a sequence point. The exact text in the standard regarding sequence points is in section 1.9, paragraph 7, and reads: Accessing an object designated by a volatile lvalue (3.10), modifying an object, calling a library I/O function, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. Evaluation of an expression might produce side effects. At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place. However, an overloaded operator is treated in the same way as a function call. There is a sequence point between evaluating the arguments for a function call and the actual call itself, and at the end of each statement. In the case of myint, the compiler essentially generates:

                            myint c = (a.operator++(int)).operator*(a.operator++(int));

                            The two calls to operator++ are therefore executed before the call to operator*. In the second case I would argue that the compiler appears to have generated the wrong code, because function calls are being generated, and << is left-associative. Again, the mapping is something like:

                            ((cout.operator<<(a++)).operator<<(a++)).operato

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups