Lecture #8: Operators and Expressions
Monday,
October 18, 1999
Bob
Pitha
whats an operator?
Something
that acts on a variable or an expression
Does
something
computes or
retrieves a value
changes an
object
types of operators
Arithmetic
operators
Increment/decrement
operators
Assignment
operators
Relational
operators
Equality
operators
Logical
operators
There
are others...
expressions
A
complex topic, really
For
our purposes today:
an operator
and its operands
a variable
Expressions
can be nested

expression concepts
lvalues
Conversions
Operator
precedence
lvalue
This
is a tough concept - but necessary!
Some
expressions refer to real memory (objects)
can have a
value assigned
these are
sometimes called lvalues
because they
can be on the left side of an equals sign
Some
expressions are just values
calculated,
used, then thrown away
examples
Given
the declaration
int i;
i
is an lvalue
so it is
legal to say:
i = 1;
i
+ 1 is not an lvalue
because
you cant assign a value to i
+ 1
i
+ 1 is just a computed value; it has no permanent memory associated
with it
1
is not an lvalue
you cant
assign a value to a constant
why do we care?
Some
operators require lvalue operands
Like =
legal: i = 1
illegal: i + 1 = 2
This
concept will come up again and again
conversions
What
happens here?

remember I
said the computer does integer and floating-point math differently
so how does
it mix the two?
conversions
The
answer is it finds an acceptable type, and converts whatever is necessary.
in this
example

a is converted into a double and
a+b is evaluated using floating-point addition
some caveats
Conversions
follow certain rules
depends on
the operator involved
The
old variable isnt changed
a new object
is created temporarily; you never really see it
thus, the
converted value is not an lvalue
this isnt
an issue in the simple cases weve seen so far
but it can
be in some things we have yet to see
usual arithmetic conversions
Standard
conversions applied to operands to enforce type agreement.
Most
binary operators require operands of the same type
Usually
widen, not narrow, an operand
conversion rules
Informally:
If either
operand is a long double, convert the other to long double
Otherwise,
if either operand is a double, convert the other to double
Otherwise,
if either operand is float, convert the other to float
Otherwise,
convert char and short to int
Then, if
either operand is long, convert the other to long
conversion example

ASCII character set
operator precedence and associativity
Each
expression operator in C has a precedence and associativity rule
In
a compound expression in the absence of parentheses, sometimes interpretation
is ambiguous:
a + b * c
Rules
of precedence and associativity clarify such situations
precedence
Each
operator has a precedence level
Specifies
how operands are grouped with operators in ambiguous situations
Example:
a + b * c
* has higher precedence than +, so this expression is evaluated as
a + (b * c)
associativity
Some
operators have the same precedence
In
that case, associativity rules come into play
Associativity
specifies in what order operators with equal precedence are evaluated
associativity example
a / b * c
*
and / have the same precedence
*
and / are left-associative
which means
left-to-right evaluation:
(a / b) * c
common sense
in this simple example, but less obvious sometimes
arithmetic operators
+
- * / %
Most
of these are straightforward and intuitive
These
all compute values, have no side-effects
/
Works differently for integers, real numbers: for integers, result is
truncated
the
expression 9/10 will have the value 0
%: the modulus operator

Computes
the remainder of the division
Both
operands must be integers
precedence and associativity