473,395 Members | 1,996 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.

increment operator in C++

14
how will the following code work
int a=10;

int i=a+a+++a++;
cout<<i;
the output is shown as 32 .
while if the code would have been as follows:
int i, a=10;
i=a+a+++a++;
cout<<i;
the output is 30.
why is this difference????
Mar 29 '07 #1
3 1516
JosAH
11,448 Expert 8TB
why is this difference????
You have seen a manifestation of "undefined behaviour". When you change
the value of a variable more than once before a so called "sequence point"
is reached, undefined behaviour will be the result. You increment the 'a'
variable more than once before the ';' is reached. The ';' is a sequence point.

Nothing can be said about the value of variable 'a'.

kind regards,

Jos
Mar 29 '07 #2
cuteanu
14
Thanx,
but tried many number of times, on different PC's the output remains the same.
is it something to do with the dynamic declaration of the variable i??
Mar 29 '07 #3
Banfa
9,065 Expert Mod 8TB
Thanx,
but tried many number of times, on different PC's the output remains the same.
is it something to do with the dynamic declaration of the variable i??
No as JosAH says you have invoked undefined behaviour, that is the reason for the difference. There is no need to try and explain the output further because it will be different on different platforms.

As soon as you invoke undefined behaviour in a single line of a program the behaviour of the entire program becomes undefined, that it outputs anything vaguely sensible is in fact misleading, you can not rely on the output of this program.
Mar 29 '07 #4

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

Similar topics

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: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
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...
31
by: Anjali M | last post by:
#include <stdio.h> int main() { int number = 10; printf("Number is: %d\n", number); printf("Sizeof of number is: %d\n", sizeof(number++)); printf("Number now is: %d\n", number);
8
by: Angel Tsankov | last post by:
Should pre/post increment/decrement return const or non-const? What about other functions?
5
by: per9000 | last post by:
Hi, today I experimented with the increment/++ operator. It seems the behavior depends on the compiler (I used cl/gcc + cs/mcs) and the language (I tested C and C#). Is the ++-operator well...
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
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
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
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.