Thread: pre & post increment need help

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    pre & post increment need help

    Hi all,

    I have below simple code. I tried a lot to analyze but didn't got satisfactory answer.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
            int i=10;
            i=((i++) + (++i));
            printf("i= %d\n", i);
            return 0;
    }
    the output is "i = 23"
    How come i's value became 23?
    appreciate the analysis....


    thankx

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    The simple funda with pre / post inc / dec is that


    in pre increment ++a ( the value of a is increment first and then assigned to a)
    in post increment a++ ( the value of a is asigned and then increment)

    means

    Code:
    #include <stdio.h>
    
    int main() {
      int a = 10;
      int b = a++; // 10 will be assigned to b as first assignment and then increment
      printf("b == %d\n", b);
      int c = ++a; // first increment and then assignment
      printf("c == %d\n", c);
    
      return 0;
    }
    output 10
    12

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Because 23 is the number of the magic skidoo.

    Code:
    i=((i++) + (++i));
    1. first, i is pre-incremented, so i=11
    2. then i+i = 22
    3. finally, i is post-incremented
    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
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
            i=((i++) + (++i));
    The result of the above is undefined behaviour. In other words, there is no guarantee as to what the output will be. Have a look at the following from the comp.lang.c FAQ:

    3.12a
    3.12b

    Especially read the following:

    3.8
    3.9
    3.10a
    Last edited by kermit; 10-09-2009 at 08:17 AM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kermit View Post
    The result of the above is undefined behaviour. In other words, there is no guarantee as to what the output will be. Have a look at the following from the comp.lang.c FAQ:
    That was interesting. I would never have guessed that:

    Code:
    a[i] = i++
    Would be considered undefined. Altho...

    Thanks kermi.
    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

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I wonder why this question comes up so often. It almost seems to be the first thing everyone decides to try out when first introduced to these operators. Could it be the discovery that something so simple allows one to write something so mind-blowing, since any way you look at it, it doesn't even have any obvious intuitive interpretation?
    Last edited by anon; 10-09-2009 at 08:58 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I guess it's compiled to execute left-to-right in your example.
    11 + 12 = 23
    Yet if I try printf("i= %d\n", (i++) + (++i));
    the result is
    11 + 11 = 22
    Neato!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with HTTP POST
    By Anddos in forum Networking/Device Communication
    Replies: 5
    Last Post: 03-22-2009, 08:41 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Post your Best Text Adventure
    By Joe100 in forum Game Programming
    Replies: 3
    Last Post: 08-15-2003, 05:47 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. Auto POST
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-07-2003, 10:42 AM