Please help me in understanding this code snippet-
the output is 16 16 16 but how is it working???????Code:main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); }
Please help me in understanding this code snippet-
the output is 16 16 16 but how is it working???????Code:main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); }
The code exhibits undefined behavior, which means that anything can happen; practically speaking for this code it means there is no correct output; anything the compiler generates is acceptable because you broke the rules.
Briefly, you're not allowed to modify something multiple times in between sequence points. There are lots of things that count as sequence points, but for this snippet you can think of a sequence point as happening wherever there is a semicolon.
So you have:
*f2 and a are the same object, and you're modifying it three times here without any sequence points, thus the behavior is undefined.Code:*f2 += *f2 += a += 2.5;
A number of my compilers generate code that outputs "16 16 16", but I also have one that does "8 8 8" and another that does "12 12 12". These are all "correct" in the sense that anything is correct once you've done something undefined.
Ignore the .5, it's an integer and C rounds down.
So going right to left, 16 makes sense. However, I actually get 8 8 8, which demonstrates what cas points out -- there are no standard rules for this and you cannot rely on a compiler always doing it the same.
In other words, do not count on a construction like that having a persistent outcome -- multiple assignment to the same memory location in one line like this are UNDEFINED and should not be used.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
actually the Q has been asked in written test. and there was no option like compiler dependent. Anyway thanks for the reply...
The correct answer is "undefined behaviour", which is more specific than "compiler dependent", which could mean unspecified or implementation defined.Originally Posted by rits
If this answer is not present, then "compiler dependent" is probably the next best answer, but since it is not present either, all the answers are correct, including no answer at all.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
If this is the case, be warned that some of what you're learning in this course is incorrect. Your instructor likely does not know C especially well and is teaching you a variant of C that he learned, which is tied to a particular compiler.actually the Q has been asked in written test. and there was no option like compiler dependent.
I can't be certain about this, of course, but it's nowhere near as uncommon as one might hope. Be prepared to relearn some aspects of the language after you leave this class.