Re: [Dev-C++] Question about increment..
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Mani <man...@gm...> - 2012-09-20 18:08:52
|
Hi, But this is not right, if I understand precedence rules correctly.. ++ has higher precedence than addition.. your explanation seems to say that ++ is done after addition.. In Java, the same expression gives 8.., and I believe other C++ compilers also gave 8 (I heard from others; I have not tested this myself -- I did test what happens in Java myself). I am inclined to think this looks like a bug..?? thanks, murali. On Thu, Sep 20, 2012 at 1:04 PM, a.geo <aqu...@gm...> wrote: > Well, you really are doing the next > > int a = 1, b = 2; > int c = a + b + a + b; > a++; > b++; > This is why you get 6, remember the ++ is a prefix in this case, then, is > executed NEXT to the expression. > > > int c = ++a + ++b + a + b; > Well, if this compile correctly, give you the 8 value, because > > int a = 1, b = 2; > ++a; > ++b; > int c = a + b + a + b; > > 2012/9/20 Mani <man...@gm...> >> >> 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 >> |