post and pre increment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #16
    There's a difference between C and C++ here; I assumed C was being used by the OP and quoted the C Standard text. In C none of the expressions compile because none of the (pre and post) increment operators result in an lvalue.

    C++ however treats both operators differently: the preincrement ++ operator seems to leave an lvalue; the postincrement ++ operator doesn't. While I agree that my assumption could've been totally wrong (about the language being used), the OP should've said which one actually was used.

    kind regards,

    Jos

    Comment

    • Tassos Souris
      New Member
      • Aug 2008
      • 152

      #17
      Well, clearly there was a difference in us about the language.
      I referred to C99.. and newb99 probably to a C++ Standard... jyots was probably referring to C++ too..
      Since i am not familiar with C++ standards i assumed that (since it is told C++ has compatibility with C) it would be the same...
      Just wrong of us not to clarify the language and look at both languages as we should do...

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #18
        Originally posted by Tassos Souris
        Well, clearly there was a difference in us about the language.
        I referred to C99.. and newb99 probably to a C++ Standard... jyots was probably referring to C++ too..
        Since i am not familiar with C++ standards i assumed that (since it is told C++ has compatibility with C) it would be the same...
        Just wrong of us not to clarify the language and look at both languages as we should do...
        Indeed; being a C person myself and *knowing* that none of the ++ nor -- operators yield an lvalue in any of the C versions (ranging from K&R1 up to the current version) I bluntly assumed that the OP was talking about C.

        I heartly support splitting this forum in a C and C++ forum just to get rid of the confusion (we even get C# questions here because that also starts with a C ;-)

        Assumptions are wrong of course but I fail to see what the benefit would be for the pre ++ operator yielding an lvalue:

        Code:
        int y= 42, x= 54;
        
        ++y= x;
        Great, what's the benefit of ++y being an lvalue? It ends up with the value 54 anyway, effectively annihilating the pre increment ...

        kind regards,

        Jos

        edit: I failed to find a satisfactory explanation in Bjarne Stroustrups brick of a book ...

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #19
          Originally posted by JosAH
          Great, what's the benefit of ++y being an lvalue? It ends up with the value 54 anyway, effectively annihilating the pre increment ...
          you can pass it by reference
          Code:
           void bar(int &x);
          ...
          bar(++x); // is it different than x++;bar(x); ?
          //fail: bar(x++);

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #20
            This

            y=(++x)++; //OK

            may be valid syntax and compilable in C++ but if x is a basic type, say an int, it still falls fowl of modifying a variable twice between sequence points and thus produces undefined behaviour.

            On the other hand if x is an object with an overloaded operator++ then rather than being an actual operator it is a function call and it is not undefined behaviour, as there is a sequence point "At a function return, after the return value is copied into the calling context."

            Needless to say this sort of silly coding structure is best avoided.

            Comment

            • Tassos Souris
              New Member
              • Aug 2008
              • 152

              #21
              isn't C++ fully backwards compatible with C (as much as i know.. i am not a fun of C++)?
              How can there exist such a difference?

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #22
                Originally posted by Tassos Souris
                isn't C++ fully backwards compatible with C
                Nope; this is one of the peculiarities; there are more: quite a few of them handle widening conversions and the scope of variables. C and C++ are two different languages after all (although people may not realize this).

                kind regards,

                Jos

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #23
                  No C and C++ are more like siblings than C++ a descendant of C. They have a large area of functionality that they both support but each language has things that are not part of the other Compatibility of C and C++ - Wikipedia, the free encyclopedia

                  Perhaps the easiest example of this is
                  int *pi = malloc(sizeof *pi);
                  which is completely valid C and in fact in C it would be best practice not to cast the output of malloc. This wont compile in C++ which doesn't allow assignment of non-equal pointers, it would require a cast.

                  The thing is people almost never compile C code as C++, most C++ compilers include a C compiler and when compiling a file with a .C extension compile it as C.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #24
                    Originally posted by Banfa
                    No C and C++ are more like siblings than C++ a descendant of C.
                    I didn't say that; all I said was that C and C++ are two different languages.

                    kind regards,

                    Jos

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #25
                      Originally posted by JosAH
                      I didn't say that;
                      I didn't say you did, that's my opinion, look at the post times.

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #26
                        Originally posted by Banfa
                        I didn't say you did, that's my opinion, look at the post times.
                        Well, you *could've* typed that all in two minutes ;-)

                        kind regards,

                        Jos (<--- slow typer of the year)

                        Comment

                        Working...