Thread: Confused Big time

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    1

    Confused Big time

    Can anyone pleeaaasssseee tell me how does the compiler arrive at the output of this program. My predicted answer never matches with the actual output of this program.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int n=3;
    
    
        printf("%d %d %d %d" , n, n++, ++n, n); //big confusion not able to get this one 
            
    return 0;
    }
    Please try this out on paper first before compiling. I need a detailed explanation- step by step. Thanks....

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    That is because what you are doing is undefined behaviour, so pretty much anything can happen. read here for a more thorough explanation: c++ - Undefined Behavior and Sequence Points - Stack Overflow

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The code modifies n twice in one statement. According to all versions of the C and C++ standards, that results in undefined behaviour. When something is undefined according to the standard, any result is permitted. That result might be printing some value 4 times. That result may be the output of a set of distinct values that make sense to you. The result is permitted to be a set of values that don't make sense to you. The result is permitted to be reformatting your hard drive. If your compiler produced one result, and some other compiler produces a different result, then BOTH compilers are still correct. Whatever any compiler does is correct, because there are no defined constraints on what it is permitted to do when code exhibits undefined behaviour.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Actually I'm pretty sure that the program is implementation defined, not undefined, because the comma is a sequence point. Unfortunately, where the implementation defined part comes from is because the compiler can evaluate the arguments of the function printf() in any order.

    Futher reading here: Comma Operator - C and C++ | Dream.In.Code
    Last edited by whiteflags; 08-18-2012 at 03:22 PM. Reason: new better link

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    The commas that separate function arguments are not sequence points. The comma operator is different, even though it is the same symbol. This statement is defined:
    Code:
    a++, a++;
    But this is not:
    Code:
    f(a++, a++);

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well I guess the comma in the second place is just a token then. Oh well, my point doesn't change.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by whiteflags View Post
    Actually I'm pretty sure that the program is implementation defined, not undefined, because the comma is a sequence point.
    Incorrect. The comma in function calls is not the comma operator.

    Also, C++-11 has removed references to "sequence points". The language now used in that standard reflects whether one operation is sequenced before another or not. The "or not" is explicitly called undefined behaviour now.

    Regardless of version of C or C++ standard, however, the behaviour is undefined, not implementation defined.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Oh man, if I had a dollar for every time this question was asked...
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by iMalc View Post
    Oh man, if I had a dollar for every time this question was asked...
    Pretty sure I could end world hunger with that sort of money...
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [HELP] Code to Convert Army Time to Normal Time?
    By Kipper DeVera in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2011, 11:50 PM
  2. Replies: 23
    Last Post: 05-22-2011, 11:20 PM
  3. Replies: 7
    Last Post: 11-21-2009, 01:02 AM
  4. Replies: 3
    Last Post: 06-13-2003, 06:47 AM