473,322 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Difference between pre-increment and post-increment

57
Difference between x++ and ++x;
Difference between x-- and --x;
Jun 29 '06 #1
19 139294
Banfa
9,065 Expert Mod 8TB
++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
Kasya
57
Difference between x-- and --x;
Jun 30 '06 #3
Banfa
9,065 Expert Mod 8TB
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
Kasya
57
Thanks!!!!!
Jun 30 '06 #5
maan
3
Thanks!!!!!
an other method
m+=1; is post increment
or m-=1 is post decrement
Jul 27 '07 #6
JosAH
11,448 Expert 8TB
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
maan
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
JosAH
11,448 Expert 8TB
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
mistiq
1
thank u buddy...i had the same question and ur answer really worked out:)
Nov 13 '10 #10
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
++ 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.
Oct 12 '11 #12
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
donbock
2,426 Expert 2GB
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
@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
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
donbock
2,426 Expert 2GB
@vinoth102 The initial condition for your second example should be y=5.
Jun 26 '15 #17
Sherin
77 64KB
Pre-increment

Pre increment operator is used to increment variable value by 1 before assigning the value to the variable.


Syntax:

a = ++x;


Post-increment

Post increment operator is used to increment variable value by 1 after assigning the value to the variable.


Syntax:

a = x++;
Jun 25 '20 #18
Naheedmir
62 32bit
++X is the prefix increment operator; this means that the value of x is first incremented and then used in the program. X++ is a postfix increment operator; this means that the value of x is first used in the program and then incremented. --x is prefixed decrement operator, this means that the value of x is first decremented and then used in the program. X-- is a postfix decrement operator, this means that the value of x is first used in the program and then decremented.
Jul 23 '20 #19
AjayGohil
83 64KB
Hi,

Preincrement take value and then increment.Post increment first increment and then take value

you can understand easily from below example.

open browser and run following piece of code in console.

Expand|Select|Wrap|Line Numbers
  1. var x=5;
  2. //out put:undefined
  3. var pre=++x;
  4. //output:undefined
  5. pre
  6. //output:6
  7. x
  8. //output:6
  9. var post=x++;
  10. //output:undefined
  11. post
  12. //output:6
  13.  
Oct 8 '20 #20

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

Similar topics

7
by: Rittersporn | last post by:
My humble attempt to model pre/postconditions with decorators :-) It's also my first experiment with decorators. If you have any ideas or thoughts on how to improve the code snippet, I'll be...
34
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. ...
3
by: Emil Dotchevski | last post by:
I have a table that is at 100% width, which contains a <TD> that is 80% of that. In the <TD> I want to put pre-formatted text (the output from a C++ compiler, to be more precise) and I'd like the...
21
by: Headless | last post by:
I've marked up song lyrics with the <pre> tag because it seems the most appropriate type of markup for the type of data. This results in inefficient use of horizontal space due to UA's default...
3
Pre
by: Neal | last post by:
A few questions about pre... When presenting preformatted text using the white-space: pre; property/value, Opera renders long lines at small viewport widths as exiting the borders of the...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
39
by: August Karlstrom | last post by:
Hello all, If c is a char then is there any difference between c = '\0' and c = 0
9
by: Eric Lindsay | last post by:
I can't figure how to best display little snippets of shell script using <pre>. I just got around to organising to bulk validate some of my web pages, and one of the problems occurs with Bash...
2
by: Sathyaish | last post by:
Can we have a pre-processor directive such as #define LEN 132 in C# v1.1? If so, are there any restrictions as to the scope of such a declaration?
11
by: San | last post by:
hi there, I am new to c++ and tryig to learn the basics of the c++ concepts. While I was reading the templates, I realize that the templates are a syntax that the compilar expands pased upon the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.