pre/Post Increment

This is a discussion on pre/Post Increment within the C Programming forums, part of the General Programming Boards category; from below Snippet, how i value is calculated?? Code: 1) int i = 2; int k = i++ - i++; ...

  1. #1
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Thumbs up pre/Post Increment

    from below Snippet, how i value is calculated??

    Code:
    1)
    int i = 2;
    
    int k = i++ - i++;         
    
    printf("%d %d",k i);  
    
     o/p  I m getting --->  k = 0, i = 3
    
    My doubt is ++ operator has R - L evaluation , here am doing 
    post incerement so first expression would be evaluated with original value 
    nd next value will get incremented twice so i value is 4.
    
    but am getting  i  value is 3 ???
    2)

    Code:
    int i = 2;
    
    int k = ++i - ++i;
    
    printf("%d %d",k,i);
    
    o/p i m getting ----> k = -1, i = 4
    
     Here am doing preincrement so first value will get incremented nd 
    next expression will be evaluated .My doubt is 3 - 4 or 4 - 3 ?? first 
    which variable will get incremented??(left side or right side).
    Could u plz clarify it....

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Both of your cases are undefined, as the C standard does specifically says [something like] "no variable must be updated twice within the same sequence point". Since a sequence point is roughly the same as a statement [there are places where it isn't, but we'll ignore that for now], your
    Code:
    k = i++ - i++;
    breaks that rule.

    What actually happens is that the compiler will order the increement of i and subtract independently of each other, which means that it may do:
    Code:
    k = i - i;
    i++;
    i++;
    or
    Code:
    i++;
    k = i (i++;) - i;   // Not C syntax! It does i++ after taking the first value of i.
    And likewise for the ++i variant.

    Finally, in theory, the compiler is perfectly allowed to come up with ANY numeric answer - the fact that you are getting some sort of reasonable answer is entirely based on the compiler doing "something sensible", but it's not guaranteed to do that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increment a date by 1 day?
    By fekkesh in forum C Programming
    Replies: 4
    Last Post: 03-08-2009, 03:46 AM
  2. How Does Increment Operators Work...??
    By ajayd in forum C Programming
    Replies: 37
    Last Post: 12-31-2008, 09:01 AM
  3. Increment / Decrement Operators - Help
    By shyam168 in forum C Programming
    Replies: 6
    Last Post: 03-29-2006, 08:24 PM
  4. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM
  5. low value, high value and increment
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 08:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21