424,303 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,303 IT Pros & Developers. It's quick & easy.

Difference between pre-increment and post-increment

P: 57
Difference between x++ and ++x;
Difference between x-- and --x;
Jun 29 '06 #1
Share this Question
Share on Google+
16 Replies


Banfa
Expert Mod 5K+
P: 8,916
++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

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int main(int argc, char **argp)
  5. {
  6.     int x = 5;
  7.  
  8.     printf("x=%d\n", ++x);
  9.     printf("x=%d\n", x++);
  10.     printf("x=%d\n", x);
  11.  
  12.     return EXIT_SUCCESS;
  13. }
The output of this program is

Expand|Select|Wrap|Line Numbers
  1. x=6
  2. x=6
  3. 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.
Jun 29 '06 #2

P: 57
Difference between x-- and --x;
Jun 30 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
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.
Jun 30 '06 #4

P: 57
Thanks!!!!!
Jun 30 '06 #5

P: 3
Thanks!!!!!
an other method
m+=1; is post increment
or m-=1 is post decrement
Jul 27 '07 #6

Expert 10K+
P: 11,448
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
Jul 27 '07 #7

P: 3
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....
Jul 28 '07 #8

Expert 10K+
P: 11,448
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
Jul 28 '07 #9

P: 1
thank u buddy...i had the same question and ur answer really worked out:)
Nov 13 '10 #10

P: n/a
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.
Nov 18 '10 #11

P: 5
++ 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.

The full example can be found here
Oct 12 '11 #12

P: 1
what is the output of the following programs....
Expand|Select|Wrap|Line Numbers
  1. main()
  2. {
  3.   int c,i=5;
  4.   if(i<10)
  5.   {
  6.      i++;
  7.      c=i;
  8.      cout<<c;
  9.    }
  10. }
what is the output????
Than,,,
Expand|Select|Wrap|Line Numbers
  1. main()
  2. {
  3.   int c,i=5;
  4.   if(i<10)
  5.   {
  6.      ++i;
  7.      c=i;
  8.      cout<<c;
  9.    }
  10. }
what is the output????
I think the above 2 program got the same output. them y u use a pre and post incerment opretor?????
Aug 6 '13 #13

Expert 100+
P: 2,389
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:
Expand|Select|Wrap|Line Numbers
  1. c = i++;
or
Expand|Select|Wrap|Line Numbers
  1. c = ++i;
Aug 9 '13 #14

P: 12
@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.
Aug 12 '13 #15

P: 3
x=5, y=5
x=y++ //Ans: x=5, y=6


x=5, y=6
x=++y; //Ans: x=6, y=6
Jun 25 '15 #16

Expert 100+
P: 2,389
@vinoth102 The initial condition for your second example should be y=5.
Jun 26 '15 #17

Post your reply

Sign in to post your reply or Sign up for a free account.