473,320 Members | 1,868 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,320 software developers and data experts.

preincrement and post increment evaluation problem

Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.  int a=1;
  4. printf("%d",a++ + a++);
  5. }
the output for this is 3

but when same post increment is written as
Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.  int a=1;
  4.  int j;
  5. clrscr();
  6.  j=a++ + a++;
  7. printf("%d",j);
  8. }
output is shown as 2.
can anybody clarrify?
Jul 29 '11 #1
2 2561
weaknessforcats
9,208 Expert Mod 8TB
There is no rule the compiler has to follow as to when it changes the value of a variable in a statement. Therefore, if you change the variable more then once in the same statement, different compilers will give different results. It's what is called indeterminate results.
Jul 29 '11 #2
Banfa
9,065 Expert Mod 8TB
Not just different compilers and its not just changing the value more than once. Accessing a variable more than once between sequence points where 1 or more of the accesses is to change the variables value results in undefined behaviour. So a + a++is as undefined as a++ + a++

Since the behaviour is undefined in theory (or though rarely in practice) the result could be different every-time you run the program.
Jul 31 '11 #3

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...
18
by: eddiew_AUS | last post by:
while playing around to check <blah> for my personal project, I discovered an anomaly with my code, compiled using (cringe) visual studio ver 6: #include<iostream> using namespace std; ...
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++;
8
by: Angel Tsankov | last post by:
Should pre/post increment/decrement return const or non-const? What about other functions?
1
by: mohsin | last post by:
hi everyone well i m little bit confused about the use of pre an post increment infact my thinking contradict with the logic of programe lets consider an example x=5; y=x++; z=x; after the...
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...
2
by: psmita | last post by:
Hi, Can anyone explain me the major diference in overloading preincrement and post increment operators? Regards, Smita
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...
3
by: Stang1 | last post by:
The following statement: line_buf = ' '; is equivalent to: line_buf = ' '; line_len++;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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.