i came across a peculiar problem in C:
------------------------------------------------------------------------------------------------------------------------ float a = 2.5, b = 2.5;
printf ( " %f " , b++ / a + b-- ) ; ------------------------------------------------------------------------------------------------------------------------
the weirdest part is that it gives different answers depending on the compiler used:
gcc (2.95.2) :- 3.5 turbo C :- 4.5 jdk1.4 (gnu/linux) :- 4.5
my problem is, if such / similar question arises in gate (IIT entrance exam for M.Tech), what should i write?
any help regarding the actual concept of solving problems similar to the above will be greatly appreciated.
p.s.: Let Us C - yeshwant kanithkar -> appendix A (precedence of operators) -> increment/decrement operators, says that increment/decrement operators act from right to left. what does that mean?
thank you for all the help ;-)
------------------------------------------------------
my problem is, if such / similar question arises in gate (IIT entrance exam for M.Tech), what should i write?
[SNIP]
You should write that the answer to this situation is uncertain.
any help regarding the actual concept of solving problems similar to the above will be greatly appreciated.
[SNIP]
Use brackets to tell the compiler about the precedence else compilers will keep on giving ambiguous answeres.
p.s.: Let Us C - yeshwant kanithkar -> appendix A (precedence of operators) -> increment/decrement operators, says that increment/decrement operators act from right to left. what does that mean?
__________________________________ Do you Yahoo!? The New Yahoo! Shopping - with improved product search http://shopping.yahoo.com
psawant@nse.co.in wrote:
float a = 2.5, b = 2.5;
printf ( " %f " , b++ / a + b-- ) ;
gcc 3.2.2 warns that the "operation on `b' may be undefined". That's the answer you're looking for
jdk1.4 (gnu/linux) :- 4.5
What has jdk1.4 got to do with C?
p.s.: Let Us C - yeshwant kanithkar
[snip]
Grab a copy of _The C Programming Language_ 2/e by Brian Kernighan and Dennis Ritchie and read it cover to cover. That would equip you with all the ammo you need to tackle such questions. The book should cost you under Rs 100/- first hand after discounts.
[Note, I cannot quote your text because it came in as an attachment]
The C standard prohibits modifying the same variable twice within a single checkpoint, so the result of a++ - --a is undefined. I'm not sure of the exact definition of a checkpoint, but it's safe to assume that it does not exceed a single statement (;).
Philip