Increment/decrement question

Discussion in 'C Programming' started by Kevin Walzer, Oct 15, 2007.

  1. Kevin Walzer

    Kevin Walzer Guest

    This code:

    #include <stdio.h>

    int main(void) {

    int n1, n2; //two integers

    n1 = 1;
    n2 = 1;

    printf("At first, n1 is %d, n2 is %d.\n", n1, n2);

    n2 = n1++;


    printf("After n2 = n1++, n1 is %d, n2 is %d.\n", n1, n2);

    n2 = n1--;

    printf("After n2 = n1--, n1 is %d, n2 is %d.\n\n", n1, n2);

    return 0;
    }

    yields this output:

    At first, n1 is 1, n2 is 1.
    After n2 = n1++, n1 is 2, n2 is 1.
    After n2 = n1--, n1 is 1, n2 is 2.


    Question:

    I would expect the statement:

    n2 = n1++

    to be equivalent to

    n2=(n1 +1),

    which, given that n1 = 1, should return a value of 2.

    So why is n1's value 2 but n2's value is 1?
    Kevin Walzer, Oct 15, 2007
    #1
    1. Advertising

  2. Kevin Walzer

    cipher Guest

    On 15 Okt., 16:44, Kevin Walzer <> wrote:
    > This code:
    >
    > #include <stdio.h>
    >
    > int main(void) {
    >
    > int n1, n2; //two integers
    >
    > n1 = 1;
    > n2 = 1;
    >
    > printf("At first, n1 is %d, n2 is %d.\n", n1, n2);
    >
    > n2 = n1++;
    >
    > printf("After n2 = n1++, n1 is %d, n2 is %d.\n", n1, n2);
    >
    > n2 = n1--;
    >
    > printf("After n2 = n1--, n1 is %d, n2 is %d.\n\n", n1, n2);
    >
    > return 0;
    > }
    >
    > yields this output:
    >
    > At first, n1 is 1, n2 is 1.
    > After n2 = n1++, n1 is 2, n2 is 1.
    > After n2 = n1--, n1 is 1, n2 is 2.
    >
    > Question:
    >
    > I would expect the statement:
    >
    > n2 = n1++
    >
    > to be equivalent to
    >
    > n2=(n1 +1),
    >
    > which, given that n1 = 1, should return a value of 2.
    >
    > So why is n1's value 2 but n2's value is 1?


    Hi Kevin,

    n1++ and n1-- are called post-increment / post-decrement operators. If
    you write
    n2 = n1++, then value of n1 gets incremented / decremented *after*
    assigning it's value to n2.
    ++n1 and --n1 is also possible, but with a little difference:
    n2 = ++n1
    first increments n1 and then assigns the new value of n1 to n2 (pre-
    increment / pre-decrement operators).

    Greetings,

    Markus
    cipher, Oct 15, 2007
    #2
    1. Advertising

  3. Kevin Walzer

    Guest

    Kevin Walzer wrote:
    ....
    > I would expect the statement:
    >
    > n2 = n1++
    >
    > to be equivalent to
    >
    > n2=(n1 +1),


    Re-read your textbook, and try to understand the difference between n1+
    + and ++n1. Once you understand that difference, the behavior of your
    program will be much clearer.
    , Oct 15, 2007
    #3
  4. Kevin Walzer

    husterk Guest

    On Oct 15, 10:44 am, Kevin Walzer <> wrote:
    > This code:
    >
    > #include <stdio.h>
    >
    > int main(void) {
    >
    > int n1, n2; //two integers
    >
    > n1 = 1;
    > n2 = 1;
    >
    > printf("At first, n1 is %d, n2 is %d.\n", n1, n2);
    >
    > n2 = n1++;
    >
    > printf("After n2 = n1++, n1 is %d, n2 is %d.\n", n1, n2);
    >
    > n2 = n1--;
    >
    > printf("After n2 = n1--, n1 is %d, n2 is %d.\n\n", n1, n2);
    >
    > return 0;
    > }
    >
    > yields this output:
    >
    > At first, n1 is 1, n2 is 1.
    > After n2 = n1++, n1 is 2, n2 is 1.
    > After n2 = n1--, n1 is 1, n2 is 2.
    >
    > Question:
    >
    > I would expect the statement:
    >
    > n2 = n1++
    >
    > to be equivalent to
    >
    > n2=(n1 +1),
    >
    > which, given that n1 = 1, should return a value of 2.
    >
    > So why is n1's value 2 but n2's value is 1?


    Kevin,

    This is an order of operations issue. In C, you can either perform a
    pre-increment (i.e. ++n) or a post increment(i.e. n++). The post
    increment gets performed after you have updated the n2 variable value.
    Try putting parentheses around the increment and decrement operations
    and that should fix your code.

    example:
    n2 = n1++; results in n2 = n1 then n1 is incremented.
    n2 = ++n1; results in n1 is incremented then n2 = n1.
    n2 = (n1++); results in n1 is incremented then n2 = n1.

    Hope this helps,

    Keith
    http://www.doubleblackdesign.com
    husterk, Oct 15, 2007
    #4
  5. Kevin Walzer

    Kevin Walzer Guest

    Kevin Walzer wrote:
    > This code:


    <snip>

    Got it--thanks to all for the quick correction.
    Kevin Walzer, Oct 15, 2007
    #5
  6. husterk said:

    <snip>
    > Try putting parentheses around the increment and decrement operations
    > and that should fix your code.


    You might want to try compiling that before asserting it. :)

    > example:
    > n2 = n1++; results in n2 = n1 then n1 is incremented.
    > n2 = ++n1; results in n1 is incremented then n2 = n1.
    > n2 = (n1++); results in n1 is incremented then n2 = n1.


    No, n2 = (n1++); is equivalent to n2 = n1++;

    Run this program:

    #include <stdio.h>

    int main(void)
    {
    int n1 = 1;
    int n2 = 1;
    n2 = (n1++);
    printf("n1 = %d, n2 = %d\n", n1, n2);
    return 0;
    }

    If you are right, this will print n1 = 2, n2 = 2

    But it doesn't.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Oct 15, 2007
    #6
  7. Kevin Walzer

    Mark Bluemel Guest

    husterk wrote:
    > On Oct 15, 10:44 am, Kevin Walzer <> wrote:

    ....

    >> I would expect the statement:
    >>
    >> n2 = n1++
    >>
    >> to be equivalent to
    >>
    >> n2=(n1 +1),


    > This is an order of operations issue. In C, you can either perform a
    > pre-increment (i.e. ++n) or a post increment(i.e. n++). The post
    > increment gets performed after you have updated the n2 variable value.
    > Try putting parentheses around the increment and decrement operations
    > and that should fix your code.


    Nope.

    > example:
    > n2 = n1++; results in n2 = n1 then n1 is incremented.
    > n2 = ++n1; results in n1 is incremented then n2 = n1.
    > n2 = (n1++); results in n1 is incremented then n2 = n1.


    Wrong. The value of (n1++) is the value of n1 before the increment...

    Just as people asking questions should cut and paste their real code,
    it's probably helpful if those who answer with source code would do the
    same - compiling it and running it before posting.
    Mark Bluemel, Oct 15, 2007
    #7
  8. Kevin Walzer

    husterk Guest

    On Oct 15, 11:08 am, Mark Bluemel <> wrote:
    > husterk wrote:
    > > On Oct 15, 10:44 am, Kevin Walzer <> wrote:

    >
    > ...
    >
    > >> I would expect the statement:

    >
    > >> n2 = n1++

    >
    > >> to be equivalent to

    >
    > >> n2=(n1 +1),

    > > This is an order of operations issue. In C, you can either perform a
    > > pre-increment (i.e. ++n) or a post increment(i.e. n++). The post
    > > increment gets performed after you have updated the n2 variable value.
    > > Try putting parentheses around the increment and decrement operations
    > > and that should fix your code.

    >
    > Nope.
    >
    > > example:
    > > n2 = n1++; results in n2 = n1 then n1 is incremented.
    > > n2 = ++n1; results in n1 is incremented then n2 = n1.
    > > n2 = (n1++); results in n1 is incremented then n2 = n1.

    >
    > Wrong. The value of (n1++) is the value of n1 before the increment...
    >
    > Just as people asking questions should cut and paste their real code,
    > it's probably helpful if those who answer with source code would do the
    > same - compiling it and running it before posting.


    Mark,

    I stand corrected. I was in a hurry and did not have a chance to
    compile my statements before posting them. I will make sure to double
    check before I post next time. Thanks for the update.

    Keith
    http://www.doubleblackdesign.com
    husterk, Oct 15, 2007
    #8
  9. cipher wrote:
    [...]
    > n1++ and n1-- are called post-increment / post-decrement operators. If
    > you write
    > n2 = n1++, then value of n1 gets incremented / decremented *after*
    > assigning it's value to n2.


    Nit-pick time:

    Technically, "n2 = n1++" will increment n1 at some point before the
    next sequence point, and n2 will get assigned the value of n1 before
    the increment took place. The chronological order is not specified.

    For example, it's perfectly reasonable for the compiler to generate
    something like this pseudo-code:

    LOAD R1,n1 ; Get the current n1 value
    INCR n1 ; Increment n1
    STOR R1,n2 ; Set n2 to the pre-incremented value

    > ++n1 and --n1 is also possible, but with a little difference:
    > n2 = ++n1
    > first increments n1 and then assigns the new value of n1 to n2 (pre-
    > increment / pre-decrement operators).


    Same nit-pick here.

    However, the result is the same "as if" things took place in the
    order you said.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Oct 15, 2007
    #9
  10. Kevin Walzer

    Richard Guest

    Kenneth Brody <> writes:

    > cipher wrote:
    > [...]
    >> n1++ and n1-- are called post-increment / post-decrement operators. If
    >> you write
    >> n2 = n1++, then value of n1 gets incremented / decremented *after*
    >> assigning it's value to n2.

    >
    > Nit-pick time:
    >
    > Technically, "n2 = n1++" will increment n1 at some point before the
    > next sequence point, and n2 will get assigned the value of n1 before
    > the increment took place. The chronological order is not specified.
    >
    > For example, it's perfectly reasonable for the compiler to generate
    > something like this pseudo-code:
    >
    > LOAD R1,n1 ; Get the current n1 value
    > INCR n1 ; Increment n1
    > STOR R1,n2 ; Set n2 to the pre-incremented value


    So what? What on earth has that to do with the price of coal when
    discussing Ansi C? It is totally compiler / platform dependant and does
    nothing but confuse the issue.

    The line of code

    n2=n1++;

    has a very definite meaning in the context it is used here in this
    example.

    >
    >> ++n1 and --n1 is also possible, but with a little difference:
    >> n2 = ++n1
    >> first increments n1 and then assigns the new value of n1 to n2 (pre-
    >> increment / pre-decrement operators).

    >
    > Same nit-pick here.
    >
    > However, the result is the same "as if" things took place in the
    > order you said.


    They do take place in the sequence he said. For the purposes of learning
    C.
    Richard, Oct 15, 2007
    #10
  11. Kevin Walzer

    Willem Guest

    Kenneth wrote:
    ) For example, it's perfectly reasonable for the compiler to generate
    ) something like this pseudo-code:
    )
    ) LOAD R1,n1 ; Get the current n1 value
    ) INCR n1 ; Increment n1
    ) STOR R1,n2 ; Set n2 to the pre-incremented value

    Compilers can do a lot worse. I've seen an optimizer effectively translate
    n2 = n1++;
    to
    n2 = (++n1)-1;

    Or in pseudo-assembly:

    INCR n1
    MOVE n2, (n1 -1)

    The second instruction is one that assigns a register to another register,
    while at the same time adding something to it. 'LEAL' on x86, iirc.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Oct 15, 2007
    #11
  12. Richard wrote:
    >
    > Kenneth Brody <> writes:
    >
    > > cipher wrote:
    > > [...]
    > >> n1++ and n1-- are called post-increment / post-decrement operators. If
    > >> you write
    > >> n2 = n1++, then value of n1 gets incremented / decremented *after*
    > >> assigning it's value to n2.

    > >
    > > Nit-pick time:
    > >
    > > Technically, "n2 = n1++" will increment n1 at some point before the
    > > next sequence point, and n2 will get assigned the value of n1 before
    > > the increment took place. The chronological order is not specified.


    [... snip pseudo-assemply for different chronological order ...]

    > So what? What on earth has that to do with the price of coal when
    > discussing Ansi C? It is totally compiler / platform dependant and does
    > nothing but confuse the issue.
    >
    > The line of code
    >
    > n2=n1++;
    >
    > has a very definite meaning in the context it is used here in this
    > example.

    [...]
    > > However, the result is the same "as if" things took place in the
    > > order you said.

    >
    > They do take place in the sequence he said. For the purposes of learning
    > C.


    If you describe "n2 = n1++;" as "n1 gets incremented *after*
    assigning its value to n2", then it confuses the issue, because
    you definitely would not say the same thing for "n1 = n1++;" or
    even "n2 = n1++ + n1++;"

    To imply (nay, flat out state) that the increment takes place
    _after_ the assignment does the newbie a disservice, IMO.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Oct 15, 2007
    #12
  13. On Mon, 15 Oct 2007 10:44:31 -0400, Kevin Walzer <>
    wrote:

    >This code:
    >
    >#include <stdio.h>
    >
    >int main(void) {
    >
    >int n1, n2; //two integers
    >
    > n1 = 1;
    > n2 = 1;
    >
    > printf("At first, n1 is %d, n2 is %d.\n", n1, n2);
    >
    > n2 = n1++;
    >
    >
    > printf("After n2 = n1++, n1 is %d, n2 is %d.\n", n1, n2);
    >
    > n2 = n1--;
    >
    > printf("After n2 = n1--, n1 is %d, n2 is %d.\n\n", n1, n2);
    >
    > return 0;
    > }
    >
    >yields this output:
    >
    >At first, n1 is 1, n2 is 1.
    >After n2 = n1++, n1 is 2, n2 is 1.
    >After n2 = n1--, n1 is 1, n2 is 2.
    >
    >
    >Question:
    >
    >I would expect the statement:
    >
    >n2 = n1++
    >
    >to be equivalent to
    >
    >n2=(n1 +1),


    It is actually closer to
    n2 = n1;
    n1 += 1;

    To be more precise, n1++ is an expression that evaluates to the
    original (unincremented) value of n1. The ++ post-increment operator
    also has a side effect of incrementing n1. There is no specified
    temporal relationship between the two; either can occur first.

    >
    >which, given that n1 = 1, should return a value of 2.
    >
    >So why is n1's value 2 but n2's value is 1?



    Remove del for email
    Barry Schwarz, Oct 18, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrix
    Replies:
    1
    Views:
    1,676
    Ivan Vecerina
    Jun 29, 2003
  2. Mark Turney
    Replies:
    11
    Views:
    4,278
    dibeas
    Nov 13, 2006
  3. John
    Replies:
    4
    Views:
    1,138
    Vladimir Marko
    May 17, 2005
  4. Ian Pilcher

    Increment, decrement, overflow, and underflow

    Ian Pilcher, Jan 20, 2005, in forum: C Programming
    Replies:
    5
    Views:
    578
    CBFalconer
    Jan 21, 2005
  5. lovecreatesbeauty
    Replies:
    8
    Views:
    1,636
    Old Wolf
    Sep 12, 2005
Loading...

Share This Page