Thread: increment and decrement operators in c

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

    increment and decrement operators in c

    please tell what are the basic rules to solve problems related to increment and decrement operators in c.
    tell about the priorities n how to solve questions.
    for eg please solve:

    if x=8
    y=x++ - --x + ++x - x--
    what is the value of y?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    There is no way to properly solve your equation because it is invoking undefined behavior. You can't increment or decrement the same variable in the same sequence point.

    Jim

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    if i write --x then first the deduction is done and then the program goes on.If i write x++ the program goes on and then adds.
    So y has the value zero.
    Because the above equality is equivalent to words like this
    "y =
    x++ ---> set y equal to x and then increase x by one (so y is now 8 and x 9)
    - --x ---> now first decrease value of x by one and then deduct from y( so y is set to zero and x to 8)
    + ++x ---> now first increase value of x by one and then add it to y(so y is set to 9 and x to 9)
    - x-- ---> now first deduct x from y and then decrease value of x by one(so y is set to 0 and x to 8)

    Another interesting example i think is this :
    Code:
    int f(int x)
    {
          printf("x = %d\n",x);
    }
    int main(void)
    {
         int x=8;
         f(x++);
         f(++x);
         f(x--);
         printf("x = %d\n",x);
         return 0;
    }
    I suggest you think which the output is going to be and then compile it and run it to see by yourself. Hope i helped a bit

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by jimblumberg View Post
    There is no way to properly solve your equation because it is invoking undefined behavior. You can't increment or decrement the same variable in the same sequence point.

    Jim
    The behavior is not undefined.If you calculate the result 1000 times,the result is going to be the same every time!
    However it is better to avoid such equations.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Another interesting example i think is this :
    You achieve consistency by introducing a whole load of sequence points ( all those ; )
    Since you are only modifying each variable only ONCE between sequence points, your example is well-defined.

    The OP's code on the other hand is a pile of crap.
    Check out my experiment on UB code compiled with many different compilers (and compiler flags) and marvel in the range of different answers obtained.

    > x++ ---> set y equal to x and then increase x by one (so y is now 8 and x 9)
    > - --x ---> now first decrease value of x by one and then deduct from y( so y is set to zero and x to 8)
    > + ++x ---> now first increase value of x by one and then add it to y(so y is set to 9 and x to 9)
    > - x-- ---> now first deduct x from y and then decrease value of x by one(so y is set to 0 and x to
    Now study my results carefully, and note that L->R evaluation is not guaranteed, specified or assumed at any point.
    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.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by std10093 View Post
    The behavior is not undefined.If you calculate the result 1000 times,the result is going to be the same every time!
    Sorry, but you are the one in error, not Jim.

    The code might happen to work consistently for you (with your compiler). It is not guaranteed to work the same way (or even at all) with other compilers, or even with a different version or configuration of your compiler.

    The definition of undefined behaviour in the standard is (informally) that anything is allowed to happen. "Anything" can include producing results that you expect, or (in your case) results that lead an uninformed programmer to believe the code has well defined 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.

  7. #7
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    This is undefined behaviour. Consistency for you is not an indication that it is defined behaviour.

    This is like saying that on the first run of your program, rand() returns 27. Always. Every time. And therefore it must be the same on every computer in the world. The specification does NOT say that. So anything that calls itself a C compiler could happily do something completely different and still be compliant. And behaviours like that can, as grumpy pointed out, change from compiler to compiler, from version to version (e.g. gcc3 is very different to gcc4 and broke a lot of software that was relying on undefined behaviour), from platform to platform (gcc on 32-bit doesn't work the same as gcc on 64-bit, etc.) and even optimisation level to optimisation level (which is, I think, what will mostly kill this line from working consistently in practice).

    If you want to increment or decrement like this, write each step on a separate line (e.g.:

    Code:
    if(x==8)
    {
       y = x++;
       y -= --x;
       y += ++x;
       y -= x--;
    }
    and the code will DEFINITELY do things in the order you specified. As soon as you put them on one line, the compiler is free to choose whatever the hell it wants to do - note that this is because, as grumpy said, you are modifying the same variables twice - if you weren't, the results would be defined (so long as you didn't do anything else silly). Basically the compiler is able to do whatever ++'s or --'s it wants first when there are multiple things affecting the same variable on the same line. There is no written (or unwritten) rule that says it will evaluate them left-to-right (which would kill performance of something that could otherwise be parallel-executed quite easily) in this particular case even if lots of SIMILAR cases do have such rules in the specification.

    It's a bit like the issue with short-circuit evaluation, e.g.:

    Code:
    if( f() && g() )
    will mean that g will NEVER be executed if f() returns FALSE. The difference is that this example is well-defined in the specification, but your multiple-incrementation of the same variable on a single line is NOT (and actually, there are specific warnings against doing that everywhere you read about it).

    Don't do it. If this is an exercise question, work it out for yourself as the teacher "wants" you to and point out that it's absolutely, totally, wrong for standardised C.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Of course i agree that this style of code should not be used.
    However i thought about it as ledow wrote it(every step in seperate lines).I see what you are trying to say and thanks a lot because i HAVE SO MANY YET TO LEARN
    (and with your posts you help me learn,thanks again)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increment / Decrement Operators
    By capricorn in forum C Programming
    Replies: 1
    Last Post: 12-16-2010, 09:35 PM
  2. Increment / Decrement Operators - Help
    By shyam168 in forum C Programming
    Replies: 6
    Last Post: 03-29-2006, 09:24 PM
  3. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 03:45 PM
  4. increment and decrement operators
    By ee0u22ba in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2003, 04:57 AM
  5. increment/decrement operators
    By ZakkWylde969 in forum C++ Programming
    Replies: 10
    Last Post: 07-10-2003, 04:17 PM