August 12th, 2012, 03:53 AM
Problems in c
Hi! I am new to c and I am facing lols of problems. Here I am introducing one of those problems and I really need help to understand how the compiler compile the following:
output is : 4
int main ()
int i = 0, j;
j =i + ++i + ++i;
printf ("%d\n", j);
Why the output is 4 and how? What is going on in j = i + ++i + ++i;
August 12th, 2012, 05:08 AM
August 12th, 2012, 07:08 AM
Why do you care? You simply should not write code like that; it is easy to avoid. In fact you have to try extra hard just to write such pointless code.
This is exactly the kind of pointless question beloved of setters of C homework assignments, and therefore your question looks like a thinly disguised attempt to get your homework done.
I sympathise with you if you have a tutor who thinks that this is a useful assignment, but all you need learn from this is that the rules regarding expression evaluation and sequence points with post- and pre- increment/decrement are sufficiently arcane for you to know to avoid using them in that way.
Ask yourself; when would I ever want to write code like that and for what purpose?
Follow this simple rule:
"never use post or pre increment/decrement in an expression involving any other operators or more than one variable."
This restricts usage to expressions of the form:
and in C at least means that it makes no difference whether your use pre-or post form, so choose one and stick to that exclusively.
The thing is ++ and -- are assignment operators. They modify the argument, and to use an argument that will change during expression evaluation is a path to all sorts of confusion - the ambiguity being when will the variable change value? The solution is not to answer the question, but rather to avoid the problem.
Your original expression may have intended:
j = i + i + i ;
j = i + (i + 1) + (i + 2) ;
j = i + (i + 1) + (i + 1) ;
Or something else altogether. Who knows? It is not clear what the writer intended so it is not good code.
j = i + (i + 2) + (i + 2) ;
If the actual result obtained in this case was in fact what was intended then it would have been better written as follows:
j = i + (i + 2) + (i + 2) ; // or i * 3 + 4 ;
i = i + 2 ;
August 12th, 2012, 07:20 AM
With all due respect (refer to Woody Allen's use of the term in his movies, starting with Broadway Danny Rose), why are you attempting such an idiotically stupid thing?
j =i + ++i + ++i;
Refer to http://en.wikipedia.org/wiki/Sequence_point, as well as my own http://pgm.dwise1.net/misc/test.html. Pay especial attention to my own link.
Are you in any way familiar with the expression, "All bets are off!"? Well, when you go changing a variable's value in the middle of an expression, then "all bets are off!" Nothing is guaranteed. In other words, this is something that you should never ever even begin to dream of doing.
Please tell us just exactly why you are trying to do this thing. Seriously, who told you to attempt this abomination! Was it your instructor? Tell us! Was it your textbook? Tell us! Just who exactly is it who tempt you beginners to attempt these unspeakable abominations? We programming masters need to confront them for their unspeakable practices!
Please pay particular attention to my own treatment of this type of problem in order to understand what is happening.
And just in case you are idiotic enough to not follow my link, there are two different ways in which the variables could be treated, so when you try to pull stupid crap like this, you cannot know what the outcome will be. The purpose of standard software is that you get the same results no matter what. No matter what! Are you starting to get the idea?