Thread: help required

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    46

    Question help required

    Please help me in understanding this code snippet-

    Code:
    main()
    {
    int a=2,*f1,*f2;
    f1=f2=&a;
    *f2+=*f2+=a+=2.5;
    printf("\n%d %d %d",a,*f1,*f2);
    }
    the output is 16 16 16 but how is it working???????

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    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:
    Code:
    *f2 += *f2 += a += 2.5;
    *f2 and a are the same object, and you're modifying it three times here without any sequence points, thus the behavior is undefined.

    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.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    actually the Q has been asked in written test. and there was no option like compiler dependent. Anyway thanks for the reply...

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rits
    actually the Q has been asked in written test. and there was no option like compiler dependent.
    The correct answer is "undefined behaviour", which is more specific than "compiler dependent", which could mean unspecified or implementation defined.

    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    actually the Q has been asked in written test. and there was no option like compiler dependent.
    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.

    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.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by cas View Post
    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.
    Is his name MisterC by any chance?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Lvalue required error
    By eklavya8 in forum C Programming
    Replies: 5
    Last Post: 01-03-2009, 04:47 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  5. Determining required flops to run C code
    By mollyann in forum C Programming
    Replies: 5
    Last Post: 03-30-2005, 01:28 PM