CS210
Answer 2 9/10/01
OUTPUT:
10 (Operators 2.1)
40 (Operators 2.2)
1 (Operators 2.3)
1 (Operators 2.4)
Derivation
Operators 2.1
initially x=2 |
|
x *= 3 + 2 |
Again, follow the precedence table.
|
x *= (3+2) |
As we saw earlier, the assignment operators have lower
precedence than the arithmetic operators. (*= is an
assignment operator.)
|
(x *= (3+2)) |
|
(x*=5 ) |
Evaluating.
|
(x=x*5 ) |
Expanding the assignment to its equivalent form.
|
(x=10) |
|
10 |
|
About define.
This program begins with the line
#define PRINTX printf(%d\n",x)
Any line in a C program that begins with the
character #
is a statement to the
C preprocessor. One job done by the preprocessor
is the substitution of one string by another.
The define
statement in this program
tells the preprocessor to replace all instances of
the string PRINTX with the string printf("%d\n",x).
Operators 2.2
Initially x=10 |
|
x *= y = z = 4 |
|
x *= y = (z=4) |
In this expression all the operators are assignments, hence
associativity determines the order of binding. Assignment
operators associate from right to left.
|
x *= (y=(z=4)) |
|
(x*=(y=(z=4))) |
|
(x*=(y=4)) |
Evaluating.
|
(x*=4) |
|
40 |
|
Operators 2.3
Initially y=4, z=4 |
|
x = y == z |
|
x = (y==z) |
Often a source of confusion for programmers new to
C is the distinction between = (assignment) and
== (test for equality). From the precedence table
it can be seen that == is bound before =.
|
(x=(y==z)) |
|
(x=TRUE) |
|
(x=1) |
Relational and equality operators yield a result of
TRUE, an integer 1, or FALSE, an integer 0. (Ray's note:
Actually to be strict, FALSE is 0 and TRUE is not 0, so
a value of 3 would also be considered to be TRUE when
evaluated in a logical expression.)
|
1 |
|
Initially x=1, z=4 |
|
x == ( y = z ) |
|
(x==(y=z)) |
In this expression the assignment has been forced to
have a higher precedence than the test for equality
through the use of parentheses.
|
(x==4) |
Evaluating.
|
FALSE, or 0 |
The value of the expression is 0. Note however that the
value of x has not changed (== does not change its operands),
so PRINTX prints 1.
|