473,394 Members | 1,752 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,394 software developers and data experts.

some very simple increment/decremant operator question

hello,
I am confused with following programs snippet code about how the
answer comes? statement/expression is evaluated?

1) int i=5;
i=i++*i++*i++*i++;
why the answer is not 5*6*7*8= 1680 but its 629?

2) int val=2;
val = - --val- val--- --val;
how to evaluate this?

3) int i=5;
printf("%d %d %d %d %d\n",++i,i++,i++,i++,++i);
why ans is not 6 6 7 7 10 but its 7 6 6 6 6?
regards,
rahul.

Nov 15 '05 #1
2 1541
ra*******@gmail.com wrote:
hello,
I am confused with following programs snippet code about how the
answer comes? statement/expression is evaluated?

1) int i=5;
i=i++*i++*i++*i++;
why the answer is not 5*6*7*8= 1680 but its 629?

2) int val=2;
val = - --val- val--- --val;
how to evaluate this?

3) int i=5;
printf("%d %d %d %d %d\n",++i,i++,i++,i++,++i);
why ans is not 6 6 7 7 10 but its 7 6 6 6 6?


See Questions 3.2 and 3.3 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #2


ra*******@gmail.com wrote:
hello,
I am confused with following programs snippet code about how the
answer comes? statement/expression is evaluated?

1) int i=5;
i=i++*i++*i++*i++;
why the answer is not 5*6*7*8= 1680 but its 629?

2) int val=2;
val = - --val- val--- --val;
how to evaluate this?

3) int i=5;
printf("%d %d %d %d %d\n",++i,i++,i++,i++,++i);
why ans is not 6 6 7 7 10 but its 7 6 6 6 6?
regards,
rahul.


I should just direct you to the FAQ but I'm feeling generous today.
The short answer is that the result of any expression of the form
i=i++, i++*i++, etc., is undefined.

The long answer involves explaining how the autoincrement/decrement
operators actually work. The expression "i++" evaluates to the current
value of i; as a side affect, i is incremented by one, but exactly
*when* the side affect is applied isn't specified other than it must
happen before the next sequence point. A sequence point may be the end
of a statement, one of the Boolean operators && or ||, and (I think) a
comma operator (which is not the same thing as a comma separator in an
argument list).

So, given the statement

i = j++ * k++;

i is assigned the *current* value of j multiplied by the *current*
value of k; and sometime before the next sequence point, j and k are
incremented. The increments may happen as each expression is evaluted,
or after both j and k are evaluated but before the result is assigned
to i, or after the assignment to i. Each of the following are valid:

1. Update each object as it is evaluated; this is how most people
expect autoincrement/decrement to work, and it may actually work this
way occasionally:

t1 <- j
j <- j + 1
t2 <- k
k <- k + 1
i <- t1 * t2

2. Update objects after all expressions have been evaluated but before
assignment:

t1 <- j
t2 <- k
j <- j + 1
k <- k + 1
i <- t1 * t2

3. Update objects after all expressions have been evaluated and the
assignment has been carried out:

i <- j * k
j <- j + 1
k <- k + 1

The exact order depends on the compiler, the optimization settings, the
surrounding code, etc.

So basically, none of the expressions you provide should be expected to
yield correct results.

Nov 15 '05 #3

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

Similar topics

9
by: Enc5 | last post by:
data structures teacher gave me like 50 questions, I have a few I'm not sure about. My last c++ class was like 8 months ago and I can't remember a lot of things. If anyone could help me on any of...
22
by: lokman | last post by:
Hi, In the following code, can someone tell me the difference between *p++ and p++ ? I can see both achieve the same result. Thanks a lot !
15
by: Robert Swan | last post by:
I'd like to know why the following program outputs 1, and not 0. #include <iostream> class a { int v; public: a():v(0){} a& operator++(int) { v++;
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 // //...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
11
by: divya_rathore_ | last post by:
The code: int aaa = 100; printf("%d %d %d\n", --aaa, aaa, aaa--); printf("%d\n", aaa); prints: 99 100 100 98
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...
5
by: simudream | last post by:
//hi maybe helpful others can look at this code and //tell me why the class code won't behave like //intrinsic types with post and pre increment //Version: 1.00 #include <iostream> using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.