Increment/decrement question

K

Kevin Walzer

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?
 
C

cipher

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
 
J

jameskuyper

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.
 
H

husterk

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
 
R

Richard Heathfield

husterk said:

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.
 
M

Mark Bluemel

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.
 
H

husterk

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
 
K

Kenneth Brody

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:[email protected]>
 
R

Richard

Kenneth Brody said:
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.
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.
 
W

Willem

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
 
K

Kenneth Brody

Richard said:
Kenneth Brody said:
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:[email protected]>
 
B

Barry Schwarz

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top