## Re: [Mingw-users] Question about increment..

 Re: [Mingw-users] Question about increment.. From: Keith Marshall - 2012-09-18 21:24:27 ```On 18/09/12 17:48, Kai Tietz wrote: > The answer is that your code describes undefined-behavior for C (C++) > language. If you take a closer look to C specification, you will > learn that the pre/post-increment|decrement operator has to be > executed before next sequence-point. This is correct. > Within your calculation, the sequence-point is the assignment operator, This is incorrect. The OP's original expression: int c = a++ + b++ + a + b; has only one sequence point -- the terminal semicolon. > so order of execution of your code is undefined. And again, correct. The sequence is complete when the assignment to variable c and the two side effects of post-incrementing variables a and b have all been completed. However, each of the three effects is independent of the other two, and the order in which they must be completed is undefined. -- Regards, Keith. ```

 [Mingw-users] Question about increment.. From: Mani - 2012-09-18 16:24:55 ```Hi, I recently to try some toy things in DevC++ (version I am using is 4.9.9.2) ```
 [Mingw-users] Question about increment.. From: Mani - 2012-09-18 16:28:47 ```Hi, I recently to try some toy things in DevC++ (version I am using is 4.9.9.2) The code I had was this: int a = 1, b = 2; int c = a++ + b++ + a + b; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl; I expected the c value to be 8 (by operator precedence, ++ has higher precedence than +, so do in order: a++, then b++ then do (a++ + b++) then add the previous result and a then add the previous result and b but the answer I got was 6.. what might be the reason for this..?? thanks, murali ```
 Re: [Mingw-users] Question about increment.. From: LRN - 2012-09-18 17:20:41 ```-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 18.09.2012 20:28, Mani wrote: > Hi, > > I recently to try some toy things in DevC++ (version I am using is > 4.9.9.2) > > The code I had was this: > > int a = 1, b = 2; int c = a++ + b++ + a + b; > > cout << "a = " << a << endl; cout << "b = " << b << endl; cout << > "c = " << c << endl; > > I expected the c value to be 8 (by operator precedence, ++ has > higher precedence than +, so do in order: a++, then b++ then do > (a++ + b++) then add the previous result and a then add the > previous result and b > > but the answer I got was 6.. > > what might be the reason for this..?? > It's called "undefined behaviour" [1]. There's a good article [2] about this. Sadly, it's in Russian, but there's an image [3] in that article that neatly illustrates the point. [1] http://en.wikipedia.org/wiki/Undefined_behavior [2] http://lurkmore.to/%2B%2Bi_%2B_%2B%2Bi [3] http://lurkmore.so/images/9/95/Cpp.png -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (MingW32) Comment: Using GnuPG with Mozilla - http://www.enigmail.net/ iQEcBAEBAgAGBQJQWK1aAAoJEOs4Jb6SI2CwnJwIAILS04mfqryi388gD89MsNHQ t9CzoeDmlPri0CD6SkXBu/rn7IEoWGS135S2MJJBQGPdH2oyiR7xGmF1Fr01hBxq NGPZ46cqA0BIUxeQINH5C6Xm1Og0S7PJiN+UDmXk3flquFWLoNMj1CtjRgDYPND6 k+lIOJMvD3THpA/i2s2gvVFlJcIUMfe+i0Wel3kbqNSH0gC9FXcV0u2VIr5M0fOa +3Zd4vTuHfKt8y1krIvtqSZjP253XTzOz9nljDyiWj7U7tAzBu2FXzE9EnyYMrHT nV3IhS3LeM5KATjZN1LoVcau0XDdw4KN3ngsasndlG+KdCHZTpFeWWIZdto8Yc0= =E4KN -----END PGP SIGNATURE----- ```
 Re: [Mingw-users] Question about increment.. From: Kai Tietz - 2012-09-18 16:48:46 ```Hi, The answer is that your code describes undefined-behavior for C (C++) language. If you take a closer look to C specification, you will learn that the pre/post-increment|decrement operator has to be executed before next sequence-point. Within your calculation, the sequence-point is the assignment operator, so order of execution of your code is undefined. Regards, Kai ```
 Re: [Mingw-users] Question about increment.. From: Keith Marshall - 2012-09-18 21:24:27 ```On 18/09/12 17:48, Kai Tietz wrote: > The answer is that your code describes undefined-behavior for C (C++) > language. If you take a closer look to C specification, you will > learn that the pre/post-increment|decrement operator has to be > executed before next sequence-point. This is correct. > Within your calculation, the sequence-point is the assignment operator, This is incorrect. The OP's original expression: int c = a++ + b++ + a + b; has only one sequence point -- the terminal semicolon. > so order of execution of your code is undefined. And again, correct. The sequence is complete when the assignment to variable c and the two side effects of post-incrementing variables a and b have all been completed. However, each of the three effects is independent of the other two, and the order in which they must be completed is undefined. -- Regards, Keith. ```
 Re: [Mingw-users] Question about increment.. From: Kai Tietz - 2012-09-19 08:10:02 ```2012/9/18 Keith Marshall : > On 18/09/12 17:48, Kai Tietz wrote: >> The answer is that your code describes undefined-behavior for C (C++) >> language. If you take a closer look to C specification, you will >> learn that the pre/post-increment|decrement operator has to be >> executed before next sequence-point. > > This is correct. > >> Within your calculation, the sequence-point is the assignment operator, > > This is incorrect. The OP's original expression: > This is correct. Please see in Annex C the following description. ... "— The end of a full expression: an initializer (6.7.8); the expression in an expression statement (6.8.3); the controlling expression of a selection statement (if or switch) (6.8.4); the controlling expression of a while or do statement (6.8.5); each of the expressions of a for statement (6.8.5.3); the expression in a return statement (6.8.6.4)." > int c = a++ + b++ + a + b; We have here an initializer (6.7.8). > has only one sequence point -- the terminal semicolon. Well, the terminal semicolon itself isn't a sequence-point, but of course it describes the end of the expression. Kai ```
 Re: [Mingw-users] Question about increment.. From: Kai Tietz - 2012-09-19 08:23:19 ```Forget about initializers ... misread it. Correct, the sequence-point here is the expression itself, which is terminated by the semicolon. Kai ```
 [Mingw-users] Question about increment.. From: Mani - 2012-09-20 16:53:26 ```Hi, I was wondering if you had any comments as to how we can explain the below.. thanks, murali. ---------- Forwarded message ---------- Hi, I recently to try some toy things in DevC++ (version I am using is 4.9.9.2) The code I had was this: int a = 1, b = 2; int c = a++ + b++ + a + b; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl; I expected the c value to be 8 (by operator precedence, ++ has higher precedence than +, so do in order: a++, then b++ then do (a++ + b++) then add the previous result and a then add the previous result and b but the answer I got was 6.. what might be the reason for this..?? thanks, murali ```
 Re: [Mingw-users] Question about increment.. From: Earnie Boyd - 2012-09-20 18:06:21 ```On Thu, Sep 20, 2012 at 12:53 PM, Mani wrote: > Hi, > > I was wondering if you had any comments as to how we can explain the > below.. thanks, murali. > This was responded to by many, are you receiving list mail? -- Earnie -- https://sites.google.com/site/earnieboyd ```
 Re: [Mingw-users] Question about increment.. From: Mani - 2012-09-20 18:17:14 ```thanks for pointing out.. I had not received the original responses.. Probably I changed my settings.. In any case, thanks for all the responses.. they were very helpful.. best, murali. On Thu, Sep 20, 2012 at 2:06 PM, Earnie Boyd wrote: > On Thu, Sep 20, 2012 at 12:53 PM, Mani wrote: >> Hi, >> >> I was wondering if you had any comments as to how we can explain the >> below.. thanks, murali. >> > > This was responded to by many, are you receiving list mail? > > -- > Earnie > -- https://sites.google.com/site/earnieboyd ```