Thread: I cant understand the output

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    184

    I cant understand the output

    Hi all.

    It been a really long time ive spent time on this board and ive moved on from C and im trying to get back into it. However my memory had been very rusty.

    Could someone please help understand why i get 3 3 1 here please?


    Code:
    #include <stdio.h>
    
    int main ( int argc, char *argv[] ) {
        int x = 1;
        printf ( "%d %d %d\n", ++x, x, x++ );
        return 0;
    }

    Many thanks

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    765
    Undefined behavior.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,091
    The behaviour is undefined because you attempt to modify the same object more than once between consecutive sequence points (or you read it and also modify it separately). So, the reason why you get this output is that the compiler decided it was a nice thing to do. It might decide otherwise tomorrow, or another compiler might disagree.

    Also, you should be aware that even if you didn't try to do this to x, the order of evaluation of function arguments is unspecified, so you still shouldn't be depending on such an ordering.
    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

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    Quote Originally Posted by laserlight View Post
    The behaviour is undefined because you attempt to modify the same object more than once between consecutive sequence points (or you read it and also modify it separately). So, the reason why you get this output is that the compiler decided it was a nice thing to do. It might decide otherwise tomorrow, or another compiler might disagree.

    Also, you should be aware that even if you didn't try to do this to x, the order of evaluation of function arguments is unspecified, so you still shouldn't be depending on such an ordering.
    Many thanks laserlight. Could you please elaborate on this? What do you mean by "consecutive sequence points". How does it make it undefined when the same object being modified. Surely i can update object as many times i like?

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    999
    Philosophy is a battle against the bewitchment of our intelligence by means of language. - Wittgenstein

  6. #6
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    311

    Cool

    Code:
    int x = 1; printf( "%d %d %d\n", ++x, x, x++ );
    why i get 3 3 1 here
    "there is absolutely no guarantee that this order would be used in any other environment, in the same environment with different options selected, or even in the same environment with the exact same options selected."

    "The above invokes undefined behavior by referencing both ‘x’ and ‘x++’ in the argument list. It is not defined in which order the arguments are evaluated. Different compilers may choose different orders. A single compiler can also choose different orders at different times."

    "Therefore, it is not recommended... This means that there’s absolutely no temporal ordering in this process. The arguments can be evaluated in any order, and the process of their evaluation can be intertwined in any way."

    "If you look at the assembly instructions generated by the compiler, you can see exactly what order the compiler is choosing to use."
    Last edited by Structure; 09-26-2020 at 10:17 AM.
    "without goto we would be wtf'd"

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    Thanks very much all this is clear now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot understand the output.
    By gaurav_13191 in forum C Programming
    Replies: 4
    Last Post: 02-10-2014, 08:31 PM
  2. I don't understand this gdb output...
    By überfuzz in forum C Programming
    Replies: 2
    Last Post: 05-09-2013, 03:30 AM
  3. Cannot able to understand the output
    By raj.knd in forum C Programming
    Replies: 7
    Last Post: 12-07-2011, 06:29 AM
  4. i cant understand why i get such an output??
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 10-16-2008, 06:06 AM
  5. cannot understand this output - atoi
    By aldajlo in forum C Programming
    Replies: 11
    Last Post: 10-02-2004, 11:48 AM

Tags for this Thread