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:21:37
|
Thanks for all the comments.. I had posted to mingw-users mailing list as well, I had lots of good responses there (I had posted here w/o looking at those responses..) for those interested, you can see these at: http://sourceforge.net/mailarchive/forum.php?thread_name=5058E683.1010205%40users.sourceforge.net&forum_name=mingw-users thanks once again for your time. best, murali. On Thu, Sep 20, 2012 at 2:15 PM, Brothers, Robert <rob...@tq...> wrote: > Murali, > > I'm not a C++ programmer (use C# mostly), but I put your original code > into Visual Studio C++.net and it gave the c = 6 result as well. > > The code Mani offered ( ++a + ++b + a + b) gave the value of 10 (which I > tend to feel is understandable since the value of a and b are > incremented first then 2 + 3 + 2 + 3 = 10). > > If I write the code as: > > int a = 1, b = 2; > int c = a++ + b++; > c += a + b; > > I DO get 8 as the result. > > Also, in C#, your original code give 8 as you found in Java. > > As I said, I'm not a C++ programmer (I'm a EE not a programmer;^) > > Regards > Bob > > > > -----Original Message----- > From: Mani [mailto:man...@gm...] > Sent: Thursday, September 20, 2012 1:09 PM > To: a.geo > Cc: dev...@li... > Subject: Re: [Dev-C++] Question about increment.. > > 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 >>> > > ------------------------------------------------------------------------ > ------ > Everyone hates slow websites. So do we. > Make your web apps faster with AppDynamics > Download AppDynamics Lite for free today: > http://ad.doubleclick.net/clk;258768047;13503038;j? > http://info.appdynamics.com/FreeJavaPerformanceDownload.html > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > |