Difference between pre-increment and post-increment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kasya
    New Member
    • Jun 2006
    • 57

    Difference between pre-increment and post-increment

    Difference between x++ and ++x;
    Difference between x-- and --x;
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    ++x is pre-increment and x++ is post-increment that is in the first x is incremented before being used and in the second x is incremented after being used.

    This is most easily demonstrated with a small program

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int argc, char **argp)
    {
        int x = 5;
    
        printf("x=%d\n", ++x);
        printf("x=%d\n", x++);
        printf("x=%d\n", x);
    
        return EXIT_SUCCESS;
    }
    The output of this program is

    Code:
    x=6
    x=6
    x=7
    In the first printf statement x is incremented before being passed to printf so the value 6 is output, in the second x is passed to printf (so 6 is output) and then incremented and the 3rd printf statement just shows that post increment following the previous statement by outputting x again which now has the value 7.

    Comment

    • Kasya
      New Member
      • Jun 2006
      • 57

      #3
      Difference between x-- and --x;

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        I would have thought you could have modified the program I posted to find out, where ++ is increment -- is decrement, apart from that that act in a similar manor.

        Comment

        • Kasya
          New Member
          • Jun 2006
          • 57

          #5
          Thanks!!!!!

          Comment

          • maan
            New Member
            • Jul 2007
            • 3

            #6
            Originally posted by Kasya
            Thanks!!!!!
            an other method
            m+=1; is post increment
            or m-=1 is post decrement

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by maan
              an other method
              m+=1; is post increment
              or m-=1 is post decrement
              Have you tried it? The value of the expression "m+=1" equals "m+1" so it is
              more like a pre increment.

              kind regards,

              Jos

              Comment

              • maan
                New Member
                • Jul 2007
                • 3

                #8
                yes i tried practicaly " m+1" is eqal to "m+=1"

                "++m" is pre increment

                if you have any other idea please reply

                i'll wait 4 ur +ive response

                Regardz Maan....

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by maan
                  yes i tried practicaly " m+1" is eqal to "m+=1"

                  "++m" is pre increment
                  Erm, yes that's what I wrote; you wrote that m+= 1 had the same value as
                  the *post* increment expression (see above).

                  kind regards,

                  Jos

                  Comment

                  • mistiq
                    New Member
                    • Nov 2010
                    • 1

                    #10
                    thank u buddy...i had the same question and ur answer really worked out:)

                    Comment

                    • chandu google

                      #11
                      i++ the post increment means after the after executing the value is increased. ++i is the preincrement it is before the executing the value is increased.

                      Comment

                      • cybosofttech
                        New Member
                        • Oct 2011
                        • 5

                        #12
                        ++ operator is called increment operator . ++[variable] is called preincrement operator and [variable]++ is called post increment operator.

                        The preincrement operator first increment operator will increment the value stored in variable and then returns the stored value.
                        Last edited by Banfa; Jun 26 '20, 10:54 PM. Reason: Removed external link

                        Comment

                        • Regila Mary
                          New Member
                          • Aug 2013
                          • 1

                          #13
                          what is the output of the following programs....
                          Code:
                          main()
                          {
                            int c,i=5;
                            if(i<10)
                            {
                               i++;
                               c=i;
                               cout<<c;
                             }
                          }
                          what is the output????
                          Than,,,
                          Code:
                          main()
                          {
                            int c,i=5;
                            if(i<10)
                            {
                               ++i;
                               c=i;
                               cout<<c;
                             }
                          }
                          what is the output????
                          I think the above 2 program got the same output. them y u use a pre and post incerment opretor?????
                          Last edited by Rabbit; Aug 6 '13, 09:49 PM. Reason: Please use code tags when posting code.

                          Comment

                          • donbock
                            Recognized Expert Top Contributor
                            • Mar 2008
                            • 2427

                            #14
                            The difference between pre-increment and post-increment is whether the increment takes place before or after the value is used. Line 6 (in both snippets) does not use the value, so there is no discernible difference between the two kinds of increment.

                            To see a difference, combine lines 6 and 7 into a single line; for example:
                            Code:
                            c = i++;
                            or
                            Code:
                            c = ++i;

                            Comment

                            • AceInfinity
                              New Member
                              • Apr 2013
                              • 12

                              #15
                              @maan - No, you're wrong... m+1 is not the same as m+=1. m+1 holds the value of m after m+=1, but for m+1, the value of m will still be (m+1)-1, because that added 1 is not stored in the variable m.

                              edit: m=m+1 is the same as m+=1.

                              Comment

                              Working...