473,416 Members | 1,675 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Increment/decrement question

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?
Oct 15 '07 #1
6 2409
On 15 Okt., 16:44, Kevin Walzer <k...@codebykevin.comwrote:
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

Oct 15 '07 #2
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 '07 #3
Kevin Walzer wrote:
This code:
<snip>

Got it--thanks to all for the quick correction.
Oct 15 '07 #4
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
Oct 15 '07 #5
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
Oct 15 '07 #6
On Mon, 15 Oct 2007 10:44:31 -0400, Kevin Walzer <kw@codebykevin.com>
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
Oct 18 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a...
8
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
5
by: Ian Pilcher | last post by:
I'm trying to figure out if an increment to a variable of an integer type, followed by a decrement, (or vice versa) is guaranteed to restore the variable to its initial value, even if the first...
3
by: George Ter-Saakov | last post by:
What is the purpose of having Interlocked.Increment if it does not work with variable declared as volatile. Here is my problem, Interlocked.Increment increments the variable in thread safe...
8
by: Angel Tsankov | last post by:
Should pre/post increment/decrement return const or non-const? What about other functions?
5
by: Stuart | last post by:
Hi all, Iv'e got a page that has a mass amount of input fields, all of which require a decimal figure. To make it easier when it comes to inputting data, I'm trying to setup + and - links that...
15
by: Ryan Liu | last post by:
Hi, Is there any known bug related to Interlocked.Increment(ref var)? My client report var's value going up and down in the client/server multile-thread application. There are about 80...
13
by: jehugaleahsa | last post by:
Hello: In C++, you had to distinguish between post and pre increments when overloading. Could someone give me a short demonstration of how to write these? I get the impression that are...
3
by: Stang1 | last post by:
The following statement: line_buf = ' '; is equivalent to: line_buf = ' '; line_len++;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.