   doubt in precedences
  Hi people..I've been trying to find the logic of these precedences but I can't seem to find how the final result is 4.. can anyone help me out?#include <iostream>int main (void){   int a=9,b=1,c=2,result;   result = c-- + (b++ - --b) * a + c;   std::cout<< " Result = " << result << std::endl;   return 0;}The final result is 4 but I don't know how..Thanks in advance   VisualFX[edited by - VisualFX on February 12, 2004 10:11:28 PM][edited by - VisualFX on February 12, 2004 10:12:46 PM]
  The statement you have in your code has undefined order of execution. The way that -- and ++ go off can happen in any possible permutation of orders. So you could end up incrementing b then decrementing b and then subtracting or decrementing b then incrementing b and then subtracting. The c at the end of the statement could be the original value of c or the value after the first c was decremented. It''s all ambiguous. As a general rule of thumb: just don''t mix more than one ++ or -- in the same statement. Except in limited, special circumstances, it''s a bad night of debugging waiting to happen. ;  I suspect that it''s evaluating the b-- first, making b 0.Then it is evaluating the (b++ - b), which is 0 (since the b++ isn''t evaluated yet).Then it is multiplying the 0 by a, which is 0.Then it is adding in the left most c first (which is still 2, since the post decrement is being evaluated last), giving you 2.Then the last c is added in (which is still 2), giving you 4.Then the c is being decremented bringing c down to 1.Then the postincrement for b is being evaluated, bringing b back up to 1.This will give you:result = 4c = 1b = 1a = 9I''m not sure how it''s supposed to work, but that''s what I would guess is actually happening.  In short:pre-incrementinside the parenthesismultiplicationaddition, left-to-rightpost-increment ;  You know what is even more bizare?when I use this:#include <iostream>int main (void){int a=9,b=1,c=2,result;result = c-- + (b++ - --b) * a + c;result = c-- + (b++ - --b) * a + c;std::cout<< " Result = " << result << std::endl;system ("pause");return 0;}  it changes to " Result = 2"   lonesockPiranha are people too. ;  Actually, that makes me even more confident that it''s evaluating it the way I suggested.  c will start off as 1 on the second time through instead of 2, which will cause the result to be 2 instead of 4.I''m pretty sure it''s just evaluating all of the pre-increments first, then using the standard order of operations, then evaluating all of the post-increments. ;  It doesn't matter how the compiler is doing it because this is compiler dependent. You should not write code like this!The following is from my c++ book:Side Effects Unfortunately, C++ allows you to use side effects. A side effect is an operation that is performed in addition to the main operation executed by the statement. For example, the following is legal C++ code: size = 5; result = ++size; The first statement assigns size the value of 5. The second statement: Increments size (side effect) Assigns result the value of size (main operation) But in what order? There are three possible answers: result is assigned the value of size (5), then size is incremented. result is 5 and size is 6. size is incremented, then result is assigned the value of size (6). result is 6 and size is 6. If you don't write code like this, you don't have to worry about these sorts of questions. The correct answer is 2: The increment occurs before the assignment. However, 3 is a much better answer. The main effects of C++ are confusing enough without having to worry about side effects.  Some programmers highly value compact code. This is a holdover from the early days of computing when storage cost a significant amount of money. It is my view that the art of programming has evolved to the point where clarity is much more valuable than compactness. (Great novels, which a lot of people enjoy reading, are not written in shorthand.) C++ actually provides two forms of the ++ operator. One is variable++ and the other is ++variable. The first:number = 5;result = number++; evaluates the expression and then increments the number, so result is 5. The second:number = 5;result = ++number; increments first and then evaluates the expression. In this case result is 6. However, using ++ or -- in this way can lead to some surprising code:o = --o - o--; The problem with this is that it looks like someone is writing Morse code. The programmer doesn't read this statement; she decodes it. If you never use ++ or -- as part of any other statement, but always put them on a line by themselves, the difference between the two forms of these operators is not noticeable.  The prefix form ++variable is preferred over the suffix form variable++ because it allows the compiler to generate slightly simpler code. (Actually, the code for simple integers is not more complex, but when you get into operator overloading, you'll see that it takes more code to write the suffix version of the increment operator (variable++) than it does to write the prefix version (++variable)More complex side effects can confuse even the C++ compiler. Consider the following code fragment: value = 1; result = (value++ * 5) + (value++ * 3); This expression tells C++ to perform the following steps: Multiply value by 5 and add 1 to value. Multiply value by 3 and add 1 to value. Add the results of the two multiples together. Steps 1 and 2 are of equal priority, unlike the previous example, so the compiler can execute them in any order it wants to.Using the first method, we get a result of 11; using the second method, the result is 13. The result of this expression is ambiguous. By using the operator ++ in the middle of a larger expression, we created a problem. (This is not the only problem that ++ and -- can cause.To avoid trouble and keep programs simple, always put ++ and -- on a line by themselves.  [edited by - daveangel on February 12, 2004 11:55:33 PM];  As SiCrane said, don''t do it, it is invalid code.quote:C++ Standard, 5-4 [expr] page 63:Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place is unspecified. Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. Ther requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.Example:i = v[i++]; // the behavior is unspecifiedi = 7, i++, i++; // i becomes 9i = ++i + 1;// the behavior is unspecifiedi = i + 1;  // the value of iis incremented “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor);  hmmm so based on that, I assume the following code have undefined behaviour?i += data[i++];  (althought, it works as i intended in VS7.1, retrives value from data, adds to i and then incs i, but i''m guessing its more by luck then judgement);  Well, "X has undefined behaviour" really means, to compiler writers, "you may assume X never happens, we told the users not to do it". It''s a bit like not having a default case in a switch statement.So, yes, you may sometimes get what you want, but it''s pure dumb luck, and may not ever happen again if you ever change or upgrade your compiler.In other words, "don''t do it".   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor);  i thought as much*metal note, look closer at C++ standard*although it was working i''ve removed that offending code, better to be safe than sorry 