473,401 Members | 2,125 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,401 software developers and data experts.

The post increment operator

I have a basic doubt...
When we use the post incrment operator the incremnet of the
variable is done after the execution of the statemnt?????
for eg
int i=1;
printf("%d %d",i++,i++);

shud the answer be 1 1
or 1 2

i want to understand the execution pt
Feb 27 '07 #1
5 2682
horace1
1,510 Expert 1GB
I have a basic doubt...
When we use the post incrment operator the incremnet of the
variable is done after the execution of the statemnt?????
for eg
int i=1;
printf("%d %d",i++,i++);

shud the answer be 1 1
or 1 2

i want to understand the execution pt
the problem is that you don't know when the ++ operations are carried out (it is side effect) - they may done immediatly after the operand is used or saved until the end of the statement. For example, gcc and Borland CBuilder V5 gave
2 1

whereas Visual C 5.0 gave
1 1

in all cases the final value of i was 3

in general the advice is do not use a variable more than once in an expression if one (or more) of the references has a ++ or -- attached to it.
Feb 27 '07 #2
willakawill
1,646 1GB
I have a basic doubt...
When we use the post incrment operator the incremnet of the
variable is done after the execution of the statemnt?????
for eg
int i=1;
printf("%d %d",i++,i++);

shud the answer be 1 1
or 1 2

i want to understand the execution pt
Hi. I am getting more and more sensitive to the possibility that these questions are taken from college studies.

It is only necessary to run this code to discover the output. You can run it several times to follow how the output changes. You can insert a pre-increment, ++i, to see how that effects the order of things.

Test it and see. That is the fastest way to learn.
Feb 27 '07 #3
Sebouh
78
I'm surprised Visual C gave 1 1. I think it should be 1 2, cause this statement is broken down when the compiler generates the object file. So it's like saying:
print i : 1
i = i + 1
print i : 2
i = i + 1
Feb 27 '07 #4
horace1
1,510 Expert 1GB
I'm surprised Visual C gave 1 1. I think it should be 1 2, cause this statement is broken down when the compiler generates the object file. So it's like saying:
print i : 1
i = i + 1
print i : 2
i = i + 1
try it with different compilers and see what you get but remember the order of evaluation of operands of individual operators and the order in which side effects take place is unspecified in C / C++

see the last line of
http://www.cppreference.com/operator_precedence.html
Feb 27 '07 #5
willakawill
1,646 1GB
try it with different compilers and see what you get but remember the order of evaluation of operands of individual operators and the order in which side effects take place is unspecified in C / C++

see the last line of
http://www.cppreference.com/operator_precedence.html
Right. And vc++ 6 seems to process the parameters for printf in reverse order, i.e. right to left which is another consideration when working out the result of this syntax.
Feb 27 '07 #6

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

Similar topics

9
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a...
15
by: Robert Swan | last post by:
I'd like to know why the following program outputs 1, and not 0. #include <iostream> class a { int v; public: a():v(0){} a& operator++(int) { v++;
13
by: kailasam | last post by:
Hello, Iam having a doubt. Which is more efficient post increment or Pre increment? I have read that preincrement is efficient than Post increment. Iam not able to think how it is? For an...
18
by: Patrick Wood | last post by:
I found a problem with C# and post increments. I was going through some source code in c++ and found someone did a post increment: int x=0; for(int i=0; i<10; i++) { x = x++; }
10
by: riteshtijoriwala | last post by:
Anyone has any idea on why is there no post/pre increment operators in python ? Although the statement: ++j works but does nothing
8
by: Angel Tsankov | last post by:
Should pre/post increment/decrement return const or non-const? What about other functions?
11
by: divya_rathore_ | last post by:
The code: int aaa = 100; printf("%d %d %d\n", --aaa, aaa, aaa--); printf("%d\n", aaa); prints: 99 100 100 98
2
by: mohammad ismail aakhil | last post by:
i have overladed unary ++ both pre and postfix increment operator using member function. both the operator function differ by arguements.v use a additional argunent of type int. how does complier...
13
by: jehugaleahsa | last post by:
Hello: In C++, you had to distinguish between post and pre increments when overloading. Could someone give me a short demonstration of how to write these? I get the impression that are...
5
by: simudream | last post by:
//hi maybe helpful others can look at this code and //tell me why the class code won't behave like //intrinsic types with post and pre increment //Version: 1.00 #include <iostream> using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.