Difference between x++ and ++x;
Difference between x-- and --x;
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 - #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
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.
Difference between x-- and --x;
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.
Thanks!!!!!
an other method
m+=1; is post increment
or m-=1 is post decrement
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
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....
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
thank u buddy...i had the same question and ur answer really worked out:)
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.
++ 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.
what is the output of the following programs.... - main()
-
{
-
int c,i=5;
-
if(i<10)
-
{
-
i++;
-
c=i;
-
cout<<c;
-
}
-
}
what is the output????
Than,,, - 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?????
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:
or @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.
x=5, y=5
x=y++ //Ans: x=5, y=6
x=5, y=6
x=++y; //Ans: x=6, y=6
@vinoth102 The initial condition for your second example should be y=5.
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++;
++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.
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. - var x=5;
-
//out put:undefined
-
var pre=++x;
-
//output:undefined
-
pre
-
//output:6
-
x
-
//output:6
-
var post=x++;
-
//output:undefined
-
post
-
//output:6
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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.
...
|
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...
|
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...
|
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...
|
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 ]
|
by: August Karlstrom |
last post by:
Hello all,
If c is a char then is there any difference between
c = '\0'
and
c = 0
|
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...
|
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?
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| |