Thread: Is x=x++; Undefined Behavior?

  1. #1
    Registered User
    Join Date
    Apr 2011

    Is x=x++; Undefined Behavior?

    The output of the following code:

    x = 10;
    x = x++;
    is 11.

    What is not clear to me is: does the increment on x happen before or after the attribution? And is this considered undefined behavior?

    What about "x=++x;", is it legal C or undefined behavior?

    Update: Yep, looks like this is undefined behavior.
    Last edited by envec83; 10-03-2011 at 07:52 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault

    Apparently it's undefined so my comments about it also being pointless are...pointless.
    Last edited by MK27; 10-03-2011 at 07:51 AM. Reason: undefined
    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
    Apr 2011
    Ops. Sorry about that.

    Just to help other people coming to this thread, yeah it seems that what I asked is indeed undefined behavior.

  5. #5
    Registered User
    Join Date
    Oct 2006
    first off, a lot of things that are undefined behavior are legal C code. this seems to be one of those cases. testing this exact code on linux with gcc produces the result of 11 as you observe.

    when I turn on lots of warnings (-Wall -Wextra -pedantic) I get
    warning: operation on 'x' may be undefined [-Wsequence-point]
    so you're probably experiencing one implementation's behavior when fed this particular bit of nonsense

  6. #6
    Registered User
    Join Date
    Jun 2005
    Quote Originally Posted by Elkvis View Post
    first off, a lot of things that are undefined behavior are legal C code.
    Sorry, that is not true.

    Firstly, undefined behaviour is not C code. It is a description of the effects that code might have have.

    Undefined (as the standard defines "undefined") behaviour means that the standard defines no constraints on what effects the code produces.

    Once we get past that, we get to the question of what do you mean by "legal C code"?

    If you mean code that is guaranteed, by the standard, to produce consistent results when ported between compilers or operating systems, then any code that invokes undefined behaviour is not legal C code. Because, by definition, consistent results are not guaranteed between implementations.

    If you mean code that can be compiled without errors or warnings, then undefined behaviour is also not legal. One acceptable response by any compiler with code exhibiting undefined behaviour is issuing a diagnostic and failing to compile the code. Another compiler might not complain about the code, but that does not make the code legal by this definition.

    The only thing that is "legal" about undefined behaviour is that the compiler still conforms with the standard, no matter what it does. It can issue warnings, or not. It can successfully compile the code, or not. The code, when executed, can produce results you expect .... or not.

    If you define "legal" to mean "compiler X accepts the code without issuing diagnostics" then your definition is too narrow. The code can still fail to compile when you update the compiler.
    Last edited by grumpy; 10-04-2011 at 01:30 AM.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined behavior from VC6 to 2k5
    By m37h0d in forum C++ Programming
    Replies: 10
    Last Post: 06-22-2011, 07:56 PM
  2. openGL: textures, gluLookAt, and undefined behavior
    By MK27 in forum Game Programming
    Replies: 7
    Last Post: 04-28-2009, 10:12 AM
  3. OT:Sorry for my rude behavior
    By moussa in forum A Brief History of
    Replies: 5
    Last Post: 06-03-2008, 06:20 AM
  4. strange std::map behavior
    By manav in forum C++ Programming
    Replies: 63
    Last Post: 04-11-2008, 08:00 AM
  5. Weird cin behavior
    By Link_26 in forum C++ Programming
    Replies: 4
    Last Post: 06-25-2006, 09:25 PM

Tags for this Thread