# Help with evaluating expressions and showing their hierarchy

• 12-03-2008
newbcore
Help with evaluating expressions and showing their hierarchy
As the name suggests, I'm new and working my way through a C book for which I don't have an answer key, so I was wondering if any of you could look through my work and tell me if it's correct:

Evaluate the following expressions and show their hierarchy:

1.
Code:

`g=big/2+big*4/big-big+abc/3`
(abc=2.5, big=2, assume g to be a float)

g=2/2+2*4/2-2+2.5/3g=1+2*4/2-2+2.5/3 OPERATION: /
g=1+8/2-2+2.5/3 OPERATION: *
g=1+4-2+2.5/3 OPERATION: /
g=1+4-2+? 2.5/3=8.3333333... HOW DO FLOATS DEAL WITH THIS? THAT'S AS FAR AS I COULD GET WITH THIS ONE.

2.
Code:

`on=ink*act/2+3/2*act+2+tig`
(ink=4, act=1, tig=3.2, assume on to be an int)

on=4*1/2+3/2*1+2+3.2on=4/2+3/2*1+2+3.2 OPERATION: *
on=2+3/2*1+2+3.2 OPERATION: /
on=2+1*1+2+3.2 OPERATION: /
on=2+1+2+3.2 OPERATION: *
on=3+2+3.2 OPERATION: +
on=5+3.2 OPERATION: +
on=5.0+3.2 OPERATION: +
on=8

3.
Code:

`s=qui*add/4-6/2+2/3+6/god`
(qui=4, add=2, god=2, s is an int)

s=4*2/4-6/2+2/3+6/2s=8/4-6/2+2/3+6/2 OPERATION: *
s=2-6/2+2/3+6/2 OPERATION: /
s=2-3+2/3+6/2 OPERATION: /
s=2-3+0+6/2 OPERATION: /
s=2-3+0+3 OPERATION: /
s=-1+0+3 OPERATION: -
s=-1+3 OPERATION: +
s=2

4.
Code:

`s=1/3*a/4-6/2+2/3*6/g`
(a=4, g=3, s is an int)

s=0/4-6/2+2/3*6/3 OPERATION: *
s=0-6/2+2/3*6/3 OPERATION: /
s=0-3+2/3*6/3 OPERATION: /
s=0-3+0*6/3 OPERATION: *
s=0-3+0*6/3 OPERATION: *
s=0-3+0/3 OPERATION: /
s=0-3+0 OPERATION: -
s=-3+0 OPERATION: +
s=-3

• 12-04-2008
C_ntua
You have this (2.5 = 0.83333...)
g=1+4-2+2.5/3
g=3+0.8333... = 3.833....
Which is the answer. It actually does this:
g=3.0 + 0.8333... = 3.8333...
turning the int to float. So if you have "float operator" int the int becomes float and the operation is done normally

(The actual value of 0.8333... will be something really CLOSE to that. Read about floating point values for more information. Or just make a really small/easy program and see by yourself)

As you do here! Don't know why the above confuses you...
on=5.0+3.2 OPERATION: +
on=8.2
on=8

What operation is done first isn't known. Doesn't really matter (except for overflows, which does). The sure thing is that *, / is done first and afterwards +, -. As you do in math.
• 12-04-2008
newbcore
Thank you very much.
• 12-04-2008
newbcore
Ok, so here's another question I have. This is the expression I'm working on.

a=b=c=3+4

Am I right about these statements?
1. For the first =, the left operand is a, and the right is b or b=c=3+4. The left operand is unambiguous, and the right is not.
2. For the second =, the left operand is b or a=b, and the right is c or c=3+4. The left operand and right operand are both ambiguous.
3. For the third =, the left operand is c or a=b=c.

So, is the right operand for the third = 3, or is it 3+4? Or could it be either?
• 12-04-2008
tabstop
Assignment always works right to left, so 7 will get assigned to each variable (the value of =, as an operator, is the value that is assigned). So for the first =, a is the left operand, and the right is (b=c=3+4), and so on.
• 12-04-2008
newbcore
Thanks for the help tabstop.