473,395 Members | 1,496 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,395 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 139308
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.