C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-11-2009, 06:57 AM   #1
Registered User
 
Join Date: Nov 2009
Posts: 1
Evaluation of arithmetic expression that involves increment operators

Here is a simple code to evaulate an arithmetic expression :

a=(++a)+(++a)+(a++)+(a++) where a is an integer . Taking an initial value of a=10,we have,

a= (++10)+(++a)+(a++)+(a++)
= 11+(++11)+(a++)+(a++)
= 11+12 +(12++)+(a++)
= 11+12+12+(13++)
= 11+12+12+13
a= 48

A simple Code executed using Turbo C++3.0 on Windows OS gives an output of 50 for the same.

CODE:
Code:
#include<stdio.h>
void main()
{
int a=10;
a=(++a)+(++a)+(a++)+(a++);

printf("%d",a);

}
As already said when this code is executed the value of a is printed as 50.

Thus i am most certain that i have gone wrong in evaluating the expression myself as i have got 48 as the answer. Can anybody identify this error and tell me how this expression gets evaulated?

Thanks in advance.
abc1000 is offline   Reply With Quote
Old 11-11-2009, 07:14 AM   #2
Registered User
 
Join Date: Feb 2009
Posts: 35
you shouldnt try to work out this problem because your code is just poor and you should never have anything at all resembling that line in a real program.

why?

for the very reason that you are confused about this problem. you should only do one thing at a time. it makes your code clear. eg
Code:
x=y++;
that is terrible code. does it mean?
Code:
x=y;
y++;
or
Code:
y++;
x=y;
the answer? who cares. we could compile and run it and find out but that isnt the problem. if anyone should ever ask that question about your code, then the code is ambigious and should be fixed so that it is absolutely clear what the program isa meant to be doing.

(btw, if you are dying to know, your arithmetic is wrong. go re read what ++a and a++ do. its a little different to what you thikn they do, which is clear after looking at your arithmetic)
Brain_Child is offline   Reply With Quote
Old 11-11-2009, 08:06 AM   #3
Registered User
 
Join Date: Apr 2006
Posts: 1,193
Expressions are not necessarily evaluated left to right. So when you substitue the value of a in your evaulation, you and the implementation has a choice of when to do so. It might sub in 10 first for all the a's for example, or it might go in the order you chose, or something else.

Additionally, a++ and ++a do three things -- they fetch a, they compute a+1, and they change the value of a. There is no guarantee that all three things happen one right after another; the compiler can mix in other operations between them. This is a problem one of the operations mixed in is assignment to a, because that assignment may simply be lost when the program assigned the result of a+1 (computed based on the original a) to a.

C is made for optimizations like this. The problems here are resolved by stating that you cannot assign or change the value of a memory location more than once between two sequence points. Nor can you retrieve and assign to a memory location between two sequence points except when the retrieved value is used to compute the new value. Seqence points are either the end of an expression (usually marked by a ;, or one of the sequencing operators &&, ||, ,(comma), and ?:.
__________________
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
King Mir is offline   Reply With Quote
Old 11-11-2009, 11:27 AM   #4
Registered User
 
Join Date: Aug 2006
Posts: 76
Quote:
Originally Posted by abc1000 View Post

As already said when this code is executed the value of a is printed as 50.


Thanks in advance.
The ONLY answer, is, the result is undefined. Period. End of story.
50 is not right. 48 is not right. 32.8765 is not right. It is undefined. Trying to figure out an actual number is a 100% waste of time.
rdrast is offline   Reply With Quote
Old 11-11-2009, 11:35 AM   #5
Registered User
 
slingerland3g's Avatar
 
Join Date: Jan 2008
Location: Seattle
Posts: 476
You should never post/pre increment or decrement a variable within the same code statement. This will surely lead to undefined behavior.
slingerland3g is offline   Reply With Quote
Old 11-11-2009, 01:54 PM   #6
Registered User
 
Join Date: Nov 2008
Posts: 75
Quote:
Originally Posted by Brain_Child View Post
you shouldnt try to work out this problem because your code is just poor and you should never have anything at all resembling that line in a real program.

why?

for the very reason that you are confused about this problem. you should only do one thing at a time. it makes your code clear. eg
Code:
x=y++;
that is terrible code. does it mean?
Code:
x=y;
y++;
or
Code:
y++;
x=y;
the answer? who cares. we could compile and run it and find out but that isnt the problem. if anyone should ever ask that question about your code, then the code is ambigious and should be fixed so that it is absolutely clear what the program isa meant to be doing.

(btw, if you are dying to know, your arithmetic is wrong. go re read what ++a and a++ do. its a little different to what you thikn they do, which is clear after looking at your arithmetic)
You're wrong. The OP code is wrong, but your example is not. The code:
Code:
x=y++;
is perfectly fine and it means:
Code:
x = y;
++y;
There's no uncertainty or implementation defined behaviour about it.
MisterIO is offline   Reply With Quote
Old 11-11-2009, 02:27 PM   #7
Ex scientia vera
 
Join Date: Sep 2007
Posts: 426
Quote:
Originally Posted by MisterIO View Post
You're wrong. The OP code is wrong, but your example is not. The code:
Code:
x=y++;
is perfectly fine and it means:
Code:
x = y;
++y;
There's no uncertainty or implementation defined behaviour about it.
This guy is right. What brain_child said is complete bull......... There would be no point in having the post/pre incremement operators if they couldn't be used that way. And there is a reason you have both post and pre increment operators.
__________________
"What's up, Doc?"
"'Up' is a relative concept. It has no intrinsic value."
IceDane is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Screwy Linker Error - VC2005 Tonto C++ Programming 5 06-19-2007 02:39 PM
recursion error cchallenged C Programming 2 12-18-2006 09:15 AM
Increment / Decrement Operators - Help shyam168 C Programming 6 03-29-2006 09:24 PM
Question on l-values. Hulag C++ Programming 6 10-13-2005 04:33 PM
Please Help - Problem with Compilers toonlover C++ Programming 5 07-23-2005 10:03 AM


All times are GMT -6. The time now is 03:36 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22