Velocity Reviews > Increment/decrement question

# Increment/decrement question

Kevin Walzer
Guest
Posts: n/a

 10-15-2007
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?

cipher
Guest
Posts: n/a

 10-15-2007
On 15 Okt., 16:44, Kevin Walzer <(E-Mail Removed)> 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

jameskuyper@verizon.net
Guest
Posts: n/a

 10-15-2007
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.

husterk
Guest
Posts: n/a

 10-15-2007
On Oct 15, 10:44 am, Kevin Walzer <(E-Mail Removed)> 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

Kevin Walzer
Guest
Posts: n/a

 10-15-2007
Kevin Walzer wrote:
> This code:

<snip>

Got it--thanks to all for the quick correction.

Richard Heathfield
Guest
Posts: n/a

 10-15-2007
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

Mark Bluemel
Guest
Posts: n/a

 10-15-2007
husterk wrote:
> On Oct 15, 10:44 am, Kevin Walzer <(E-Mail Removed)> 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.

husterk
Guest
Posts: n/a

 10-15-2007
On Oct 15, 11:08 am, Mark Bluemel <(E-Mail Removed)> wrote:
> husterk wrote:
> > On Oct 15, 10:44 am, Kevin Walzer <(E-Mail Removed)> 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

Kenneth Brody
Guest
Posts: n/a

 10-15-2007
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: <(E-Mail Removed)>

Richard
Guest
Posts: n/a

 10-15-2007
Kenneth Brody <(E-Mail Removed)> 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.

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM eddiec MCSE 6 05-20-2004 06:59 AM Wayne Cisco 0 03-02-2004 07:57 PM sean ASP .Net 1 10-20-2003 12:18 PM

Advertisments