Difference between x++ and ++x;
Difference between x-- and --x;
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 - #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: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |