what is the output of the below program?
Code:int main() { int val=5; printf("%d %d %d %d",val,--val,++val,val--); return(0); } (a)3 4 6 5 (b)Compiler Dependant (c)4 4 5 5 (d)None of these
what is the output of the below program?
Code:int main() { int val=5; printf("%d %d %d %d",val,--val,++val,val--); return(0); } (a)3 4 6 5 (b)Compiler Dependant (c)4 4 5 5 (d)None of these
well, compile it...
Arduino rocks!
I know that if I compile I will get the answer. But My doubt is that as there is an option of compiler dependent, I am asking you guys if a question like this appears in any exam...which is the correct option??
I'll give you a hint:
++var will increment it before the value is read
var++ will increment after the value is read
The rule is that no variable or memory location can be modified more than once in the same expression, except when the use is separated by a sequencing operator. Further, it cannot be accessed and modified in the same expression, unless the value accessed is used to compute the new value, also excepting when the use is separated by a sequencing operator.
Last edited by King Mir; 09-16-2009 at 06:39 AM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
Show this to anyone who thinks the answer is anything other than d)
Expressions
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I think you should read the options more carefullyOriginally Posted by Salem
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Why? None of the options said "undefined behaviour".
That is NOT the same as compiler specific.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
"None of these" must be wrong, since due to undefined behaviour "3 4 6 5" or "4 4 5 5" could be the output of the program. Consequently, I reason that "Compiler Dependant" is the best answer, or else one should add another option.Originally Posted by Salem
EDIT:
On second thought... "None of these" should be admissible, since the output could indeed be neither "3 4 6 5" nor "4 4 5 5". But then you implied that the other answers were wrong, which is wrong.
Last edited by laserlight; 09-16-2009 at 10:34 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I think compiler specific is meant here is the combined category of implementation defined, and undefined. It's a reasonable use, since the behavior of undefined C code does depend on how the compiler handles it.
Also, from a realistic point of view, no compiler will do anything besides print some numbers and possibly change the value of val.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
It's undefined behaviour!
Even a drunk occasionally manages to stumble into their own house, but that just makes them lucky, not sober.
Likewise, producing one of the given answers doesn't prove a damn thing about the correctness of the answers. Reading the C standard tells you the answer, not reverse engineering a hypothesis based on observations of a single compiler.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
In other words, if you really want to be correct, then the selection of possible answers is incomplete. Saying "Show this to anyone who thinks the answer is anything other than d)" is bogus, since you should show it to someone who picks d) as the answer. Either that, or you agree that b) is the best answer, even though the other answers are correct.Originally Posted by Salem
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
There is nothing incomplete about the answers at all (logically). Option d is a catch-all else to the other 3 conditions.
Misleading and unhelpful perhaps, because there isn't a specific option of "undefined behaviour" (which would have been good). If you know the answer, don't see your answer, but do see "none of the above", what else are you supposed to choose?
The danger of course is that someone who tries the code, and gets an answer which isn't a or c then has a problem - is it compiler specific, or something else?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.