Thread: please ,need a little help on incrementing operators.

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    please ,need a little help on incrementing operators.

    Hello guys..i am beginner to c programming and i like to know what is happening in this code depth
    Code:
    #include<stdio.h>
    main(){
    int i=0;
    printf("%d %d",++i,i++);
    i=0;
    printf("\n%d",++i);
    i=0;printf("\n%d",i++);
    return 0;
    }
    output is 2 0 1 0
    How this happen ...explain to me
    Last edited by Salem; 05-11-2013 at 03:56 AM. Reason: removed colour abuse

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Try it this way:
    Code:
    #include<stdio.h>
    int main(void){
       int i=0;
       printf("%d %d\n",i++,++i);
       i=0;
       printf("%d\n",++i);
       i=0;
       printf("%d\n",i++);
       return 0;
    }
    I get
    1 1
    1
    0

    Only the first printf() is in doubt. Because you have one variable being incremented twice with no sequence point between them, you can wind up with undefined behavior.

    Bottom line, don't do that - it may work, but it may just as likely fail, on another C compiler or system. Also, there's no reason for such shenanigans.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Adak View Post
    Only the first printf() is in doubt. Because you have one variable being incremented twice with no sequence point between them, you can wind up with undefined behavior.
    The first printf() and anything after it is "in doubt".

    Once undefined behaviour is introduced into a program, anything is allowed to happen. Including, in this case, causing a subsequent statement like "i=0;" do do something different than you expect. There is nothing, for example, suggesting that the first expression only affects the value of the variable i.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Same code (with unspecified behaviour, and undefined behaviour), many different compilers, and many different answers.
    Post and Pre Increment
    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.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by grumpy View Post
    The first printf() and anything after it is "in doubt".

    Once undefined behaviour is introduced into a program, anything is allowed to happen. Including, in this case, causing a subsequent statement like "i=0;" do do something different than you expect. There is nothing, for example, suggesting that the first expression only affects the value of the variable i.
    Sory, grumpy, this one will have to fall into your 3%.
    Whilst something like
    Code:
    printf("%d\n", i++ + ++i);
    is undefined behaviour,
    Code:
    printf("%d %d",++i,i++);
    is not undefined behaviour. The order of evaluation of function arguments is "unspecified behaviour". There's an important difference.

    That means this program is okay, though it may have one of two possible results, depending to the implementation of the compiler and the order the arguments on line 4 are evaluated.
    the-difference-between-unspecified-and-undefined-behaviour
    Last edited by iMalc; 05-11-2013 at 03:31 PM.
    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"

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > is not undefined behaviour. The order of evaluation of function arguments is "unspecified behaviour". There's an important difference.
    It's still undefined behaviour!

    Code:
    printf("%d %d", foo(), bar() );
    It is unspecified behaviour as to the order in which foo and bar are called.
    This is demonstrated in my test1() code by the differing order of calls to f1 .. f5
    The computed answer is consistent (as it should be), but the intermediate steps can vary from one implementation to another.

    Code:
    printf("%d %d",++i,i++);
    This is undefined behaviour, because the same object is modified more than once between sequence points.
    This is demonstrated in my test2() code producing all sorts of weird and wonderful answers - none of which are meaningful.
    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.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by iMalc View Post
    Sory, grumpy, this one will have to fall into your 3%.
    Whilst something like
    Code:
    printf("%d\n", i++ + ++i);
    is undefined behaviour,
    Code:
    printf("%d %d",++i,i++);
    is not undefined behaviour. The order of evaluation of function arguments is "unspecified behaviour". There's an important difference.
    This is not within my (coarsely estimated) 3%.

    True, the order of evaluation of function arguments is unspecified.

    The problem is that i is being modified twice in one statement. In older versions of the standards (the description has now changed) it would be said there is no intervening sequence point between the two increments of i. That is undefined behaviour.

    The presence of undefined behaviour (where the standard imposes no bounds whatsoever on what is permitted) trumps the presence of unspecified behaviour (where the standard does bound the permitted results). When both are present - as in this case - the result is undefined, not just unspecified.
    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. Incrementing Twice?
    By Kemaratu in forum C Programming
    Replies: 3
    Last Post: 09-27-2009, 11:13 PM
  2. incrementing problems...
    By MikeyIckey in forum C Programming
    Replies: 6
    Last Post: 09-23-2007, 04:40 PM
  3. Incrementing by 2 not 1
    By Eckey in forum C++ Programming
    Replies: 6
    Last Post: 10-14-2004, 03:16 PM
  4. incrementing pointers!?!
    By pmit2 in forum C Programming
    Replies: 2
    Last Post: 03-17-2002, 12:32 PM
  5. incrementing on a specified base
    By canine in forum Windows Programming
    Replies: 2
    Last Post: 12-11-2001, 02:22 PM