# Thread: Order of operations question

1. ## Order of operations question

Code:
```#include <stdio.h>
void main()
{
float a = 4, b = 4, c = 8;
a = (a + c) / ++c;
printf("\n a = %f", a);

}```
Does the () take precedence over the pre increment or not?
In some text books the () appears higher up in the table of precedence.

I ran this program in VS 2003 and I got 1.444, which is 13 / 9.

Why don't I get (12) / 9 if the parens come first?

2. It does take higher precedence.

But you have to consider the precedence of all operators involved along with their associativity rules. You have 4 operators in effect on that formula. (), +, / and ++.

So... let's order them by precedence and associativity.

1st ()
2nd +
3rd ++
4th /

The addition operator is moved up because the grouping operator forces evaluation of the expression contained inside. So, the first thing that happens is the sum of a with c.

The pre-increment has higher precedence over the division. As such, it is evaluated first. c is incremented.

Only then is the division made.

3. > I ran this program in VS 2003 and I got 1.444, which is 13 / 9.

I didn't see this, sorry.

That may come from the fact a, b and c are floats and some manner of rounding is happening. Remember that floats have a very low precision.

Change b and c to int and a to double. the result will have a much higher precision which I believe will be acceptable for your problem

4. Originally Posted by Mario F.
So, the first thing that happens is the sum of a with c..
I don't understand. If the first thing that happens is the sum of a with c then a+c should be 12. Instead, a+c is giving 13 which implies that c has been incremented already.

What am I missing?

5. I suspect the answer is that it is undefined, though I am not entirely sure.

6. If you are using the ++ operator before the variable name, the first thing that happens is the variable is incremented. Which is the exact opposite of using ++ after the variable name, it will increment it after the other operations are preformed.

7. I've always used ++ after the variable and I have never had any problems with it.
Now when I start thinking using ++ before the variable seems to be very useful in some cases...

8. > Does the () take precedence over the pre increment or not?
Do not confuse the fact that you have used () with "This is always done first".

> In some text books the () appears higher up in the table of precedence.
Sounds like some other books are wrong then.

> I ran this program in VS 2003 and I got 1.444, which is 13 / 9.
Good for you.
Your expression is undefined, so any answer (or indeed no answer) is entirely plausable.
http://c-faq.com/expr/index.html

NEVER EVER use ++ on a variable, and attempt to use that same variable in another part of the expression. It's undefined - no if's, but's or maybe's.

Also read the part of that FAQ which talks about "sequence points".

Given
f() + g() / h();
No amount of () or operator precedence will tell the compiler which function to call first. Precedence and () only describe how sub-expressions are combined, not evaluated.
It is perfectly valid for a compiler to call f() first.

Oh yeah, the void main is wrong as well.

9. Imagine it as a series of function calls like this:
Code:
`a = divide(add(a, c), increment(c));`
C++ doesn't specify which function argument will be evaluated first, so for the divide function, you don't know whether the add(a,c) will run first or the increment(c). The result is undefined.

All the parentheses do is stop it from being this:
Code:
`a = add(a, divide(c, increment(c)));`

10. Originally Posted by Salem
> Oh yeah, the void main is wrong as well.
5275: Told ya!

Popular pages Recent additions