Precendence & order of evaluation

c

i am confused with the precedence and order of evaluation.pls explain me with an example

Best Answer

Here's a secret: I don't bother learning precedence rules. They are too easy to get wrong, and it makes the next person to look at the code think too much. To paraphrase: assume the person who maintains your code is an axe-wielding psycho who knows where you live. Be nice too them.

So rather than:

x = a + b * c / d;

I would use:

x = a + ((b * c) / d);

Or perhaps better (subjective), break it down into separate statements The trickier question, perhaps, is what happens in "clever" lines of code that has side-effects on expressions later in the line:

x = Func(z) * a[i++] + i; // where Func(z) somehow mutates i
                          // (or even worse when using "conditional" etc)

Such things should be used sparingly, and you should try to know what behaviour is defined and what is explicitly undefined but works (or fails) depending on the compiler you use.