Thread: Incrementation Questions

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Adak View Post
    But ++max IS trying to change a define, and defines are constant values.
    #define's are not constant values. They are directions to the preprocessor to perform substitution of text in source code.

    So
    Code:
    #define max 52
    int main()
    {
        ++max;
    }
    is effectively seen by the compiler - after completion of preprocessing - as
    Code:
    int main()
    {
        ++52;
    }
    which (by attempting to increment a literal value, which is not permitted) causes a compilation error.


    As to the original question, any change of a variable twice in one statement causes undefined behaviour. Anything is allowed to happen, including crashing your hard drive or producing the output that your teacher expects.

    All the teacher is doing is assuming that the various increment operators are performed in some order. To give the explanation s/he seeks, simply find an order of operations that might explain the particular output.

    However, your teacher is getting you to learn rubbish. Apart from the (hopefully remote) possibility of crashing your hard drive, the behaviour from that code is not predictable. The behaviour can change between versions of a compiler, change when you use another compiler, and all sorts of things.

    EDIT: As to whether you tell the teacher that, it depends. Some (poor) teachers do not take kindly to being legitimately corrected. Some accept that in good grace. If your teacher is one who does not take kindly to being corrected, you might be better off saying nothing, and trying to learn C properly once you get past this class.
    Last edited by grumpy; 03-22-2013 at 01:36 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. Incrementation in conditionals
    By Niels_M in forum C Programming
    Replies: 2
    Last Post: 07-20-2010, 11:19 AM
  2. Pointer incrementation
    By newbie30 in forum C Programming
    Replies: 6
    Last Post: 08-14-2009, 09:51 PM
  3. HEX-numbers after incrementation x in for-loop.
    By Jelte in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2009, 10:36 AM
  4. string::size_type incrementation
    By cunnus88 in forum C++ Programming
    Replies: 2
    Last Post: 05-01-2007, 10:59 PM

Tags for this Thread