Hey folks, that's funny...
-
there is next code:
int a; int b; a = 5; b = ++a + ++a; // Can you guess what will be the "b"?
Try it compile and run in various languages: C++ and C# and Java. I had got different results even in the same compiler (Watcom C++) -- with and without optimisation.A
-
there is next code:
int a; int b; a = 5; b = ++a + ++a; // Can you guess what will be the "b"?
Try it compile and run in various languages: C++ and C# and Java. I had got different results even in the same compiler (Watcom C++) -- with and without optimisation.A
++a is the prefix incrementor a++ is the postfix incrementor if a = 5 then ++a returns 6 and a becomes 6 if a = 5 then a++ returns 5 and a becomes 6 therefore
b = ++a + ++a
= 6 + 7
= 13
Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
-
++a is the prefix incrementor a++ is the postfix incrementor if a = 5 then ++a returns 6 and a becomes 6 if a = 5 then a++ returns 5 and a becomes 6 therefore
b = ++a + ++a
= 6 + 7
= 13
Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
You are correct and this is what i got with c# as well, but c++ gave me 14.
-
You are correct and this is what i got with c# as well, but c++ gave me 14.
I have read this statement in a book. "The results of expressions which use same variables with increment operators more than once are unpredicatble in C Language" int i=0; i=i++ + ++i; //3 not 2 ----------------- int i=0,j=0; j=i++ + ++i; //2 as esxpected You can try with lot of examples...
Regards, Arun Kumar.A
-
I have read this statement in a book. "The results of expressions which use same variables with increment operators more than once are unpredicatble in C Language" int i=0; i=i++ + ++i; //3 not 2 ----------------- int i=0,j=0; j=i++ + ++i; //2 as esxpected You can try with lot of examples...
Regards, Arun Kumar.A
It would indeed seem that way. If I do the following in c++ I do get the expected result int a; int b; a = 5; b = ++a; b += ++a;
-
It would indeed seem that way. If I do the following in c++ I do get the expected result int a; int b; a = 5; b = ++a; b += ++a;
Try to use the variable 'a' more than once(with increment/decrement operator) in a single expression and assign the result to 'a' itself. You will get unexpected results.
Regards, Arun Kumar.A
-
You are correct and this is what i got with c# as well, but c++ gave me 14.
-
Try to use the variable 'a' more than once(with increment/decrement operator) in a single expression and assign the result to 'a' itself. You will get unexpected results.
Regards, Arun Kumar.A
However. It's strange, C++ gives 14 at the same time C# gives 13. The different interpretation of statements?
A