473,788 Members | 2,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unpredictable result of Increment operation

Is there any reason why the following C++ code behaves as it does ?

int i;

i=1; cout << (++i)++; Output: 2

i=1; cout << ++(++i); Output: 3

i=1; cout << (i++)++; Output: Error. LValue required

i=1; cout << ++(i++); Output: Error. LValue required
Jun 27 '08 #1
13 1458
bintom wrote:
Is there any reason why the following C++ code behaves as it does ?

int i;

i=1; cout << (++i)++; Output: 2
Because it's nonsensical undefined behaviour.

--
Ian Collins.
Jun 27 '08 #2
Ian Collins wrote:
bintom wrote:
>Is there any reason why the following C++ code behaves as it does ?

int i;

i=1; cout << (++i)++; Output: 2
Because it's nonsensical undefined behaviour.
Why is it non-sensical? Why is it undefined?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #3
Victor Bazarov wrote:
Ian Collins wrote:
>bintom wrote:
>>Is there any reason why the following C++ code behaves as it does ?

int i;

i=1; cout << (++i)++; Output: 2
Because it's nonsensical undefined behaviour.

Why is it non-sensical? Why is it undefined?
You are right, it is neither. Sorry, I've seen way too many posts that
are and do...

--
Ian Collins.
Jun 27 '08 #4
On Thu, 8 May 2008 17:12:15 -0700 (PDT), bintom
<bi************ *@gmail.comwrot e in comp.lang.c++:
Is there any reason why the following C++ code behaves as it does ?

int i;

i=1; cout << (++i)++; Output: 2

i=1; cout << ++(++i); Output: 3

i=1; cout << (i++)++; Output: Error. LValue required

i=1; cout << ++(i++); Output: Error. LValue required
For build-in types, such as int, the behavior of the first two is
undefined. You are attempting to modify the value of 'i' more than
once without a sequence point in between. The output could be
anything, as far as the language is concerned.

The last two are errors because the post increment operator on a
built-in type does not yield an object, instead it yields the value
that the object had before the post increment. So it is the same as
if you had written:

cout << 1++;
cout << ++1;

You can't apply the increment operators to a value.

I'd suggest you consult your C++ book about sequence points and
undefined behavior.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 27 '08 #5
Hi Jack,

Thank for your inputs on undefined/invalid uses (rather than
unpredictable results) of ++ operator. It was a great help.

bintom
Jun 27 '08 #6
On May 9, 2:54 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Ian Collins wrote:
bintom wrote:
Is there any reason why the following C++ code behaves as it does ?
int i;
i=1; cout << (++i)++; Output: 2
Because it's nonsensical undefined behaviour.
Why is it non-sensical? Why is it undefined?
Because the standard says so. You're modifying the same object
twice without an intervening sequence point.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #7
James Kanze wrote:
On May 9, 2:54 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>Ian Collins wrote:
>>bintom wrote:
Is there any reason why the following C++ code behaves as it does ?
>>>int i;
>>>i=1; cout << (++i)++; Output: 2
>>Because it's nonsensical undefined behaviour.
>Why is it non-sensical? Why is it undefined?

Because the standard says so. You're modifying the same object
twice without an intervening sequence point.
Thanks. I bet you that was the explanation the OP would have been
looking for.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #8
James Kanze dixit:
On May 9, 2:54 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>Ian Collins wrote:
>>bintom wrote:
Is there any reason why the following C++ code behaves as it does ?
>>>int i;
>>>i=1; cout << (++i)++; Output: 2
>>Because it's nonsensical undefined behaviour.
>Why is it non-sensical? Why is it undefined?

Because the standard says so. You're modifying the same object
twice without an intervening sequence point.
Can you tell what is an intervening sequence point ?

Thanks
Jun 27 '08 #9
Ian Collins wrote:
Victor Bazarov wrote:
>Ian Collins wrote:
>>bintom wrote:
Is there any reason why the following C++ code behaves as it does ?

int i;

i=1; cout << (++i)++; Output: 2

Because it's nonsensical undefined behaviour.
Why is it non-sensical? Why is it undefined?
You are right, it is neither. Sorry, I've seen way too many posts that
are and do...
Um, undefined behaviour overload. I was right the first time...

--
Ian Collins.
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

98
14464
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
3
1647
by: Andrew | last post by:
Hi all , i am supposed to create a program that performs a specific operation to a 2-D matrix whose elements are positive (or zero ). This operation is repeated T times on the matrix and only the final matix is needed . The operation to be perormed is V(t,x,y)=1/2*(sqrt(V(t-1,x-1,y))+sqrt(V(t-1,x-1,y))) . The problem iis that i have (or i think i have ) created a correct program that performs this operation but something happens that i...
5
1305
by: kelmen | last post by:
Try the below simple program. It give expected result. 1 1 2 2 Then toggle the statements in private static int sinc(), to use Test._num ++ .
8
4215
by: Andrea | last post by:
I wrote this code: void * xmalloc (size_t size){ register void *value = OPENSSL_malloc(size); if (value == 0) printf("virtual memory exhausted"); return value; } int _chooseTSK(char* message,int seed){
8
2092
by: bintom | last post by:
Why doe the following C++ code behaves as it does? int i; i=1; cout << (++i)++; Output: 2 i=1; cout << ++(++i); Output: 3 i=1; cout << (i++)++; Output: Error. LValue required i=1; cout << ++(i++); Output: Error. LValue required
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9967
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.