CS210
Answer 1 9/7/01

OUTPUT:
11     (Operators 1.1)
1     (Operators 1.2)
0     (Operators 1.3)
1     (Operators 1.4)

Derivation

Operators 1.1

x = - 3 + 4 * 5 - 6 Begin by reading a C precedence table from high to low. (See Appendix C in the Oualline text).
x = (-3) + 4 * 5 - 6 The highest level operator in the expression is the unary -. We'll use parentheses to indicate the order of binding operands to operators.
x = (-3) + (4*5) - 6 Next highest in the expression is *.
x = ((-3)+(4*5)) - 6 Both + and - are at the same precedence level. The order of binding thus depends on the associativity rule for that level. For + and -, associativity is left to right. First the + is bound.
x = (((-3)+(4*5))-6) And then the -.
(x=(((-3)+(4*5))-6)) And finally, near the bottom of the precedence table, is =. Now that we have completely identified the operands for each operator, we can evaluate the expression.
(x=(-3+(4*5))-6))) For this expression, evaluation proceeds from the inside out.
(x=((-3+20)-6)) For this expression, evaluation proceeds from the inside out.
(x=(17-6))  
(x=11)  
11, an integer The value of an assignment expression is the value of the right-hand side cast in the type of the left-hand side.

About printf printf is the formatted print routine that comes as part of the standard C library. The first argument to printf is a format string. It describes how any remaining arguments are to be printed. The character % begins a print specification for an argument. In our program, %d told printf to interpret and print the next argument as a decimal number. We will see other print specifications in later programs. printf can also output literal characters. In our program, we "printed" a newline character by giving its name (\n) in the format string.

Operators 1.2

x = 3 + 4 % 5 - 6 This expression is very similar to the previous one.
x = 3 + (4%5) - 6 Following precedence
x = (3+(4%5)) - 6 and associativity
x = ((3+(4%5))-6) leads to
(x=((3+(4%5))-6)) this. (The modulo, %, operator yields the remainder of dividing 4 by 5.)
(x=((3+4)-6)) Again, evaluation is from the inside out.
(x=(7-6))  
(x=1)  
1  


Operators 1.3

x = - 3 * 4 % - 6 / 5 This expression is a bit more complex than the last, but rigorous adherence to precedence and associativity will untangle it.
x = (-3) * 4 % (-6) / 5  
x = ((-3)*4) % (-6) / 5 *, %, and / are all at the same precedence level, and they associate from left to right.
x = (((-3)*4)%(-6)) / 5  
x = ((((-3)*4)%(-6))/5)  
(x=((((-3)*4)%(-6))/5))  
(x=(((-3*4)%-6)/5)) Evaluating from the inside out.
(x=((-12%-6)/5))  
(x=(0/5))  
(x=0)  
0  


Operators 1.4

x = ( 7 + 6 ) % 5 / 2 Of course we are not totally at the mercy of predefined precedence. Parentheses can always be used to effect or clarify a meaning.
x = (7+6) % 5 / 2 Subexpressions within parentheses bind first.
x = ((7+6)%5) / 2 Then, it is according to the precedence and associativity rules as before.
x = (((7+6)%5)/2)  
(x=(((7+6)%5)/2))  
(x=((13%5)/2)) Evaluating
(x=(3/2)) Evaluating
(x=1) Integer arithmetic truncates any fractional part.
1  

A note from the author of "The Puzzle Book".

About programming style. As mentioned in the Preface, the programs in this book are not models to be copied. They were designed to make you think about the mechanics of how C works. But the puzzles do contain messages about program style. If a construct always forces you to consult a reference to find out how some detail is handled, then the construct is either not well written or should be accompanied by a comment that provides the missing details.

The message from this first set of puzzles is to use parentheses in complex expressions to help the reader associate operands with operators.