Lecture #8: Operators and Expressions

Monday, October 18, 1999

Bob Pitha

bpitha@cs.ucsd.edu

http://www-cse.ucsd.edu/classes/fa99/cse5a

what’s 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

Text Box: (a+b)*c	Operator is *, operands are (a+b) and c
(a+b)		Operator is (), operand is a+b
a+b		Operator is +, operands are a and b
And finally, a and b are also expressions
 


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 can’t 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 can’t 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?

Text Box: int a = 1;
double b = 7.35;
double c;
c = a + b;
 


–   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

Text Box: int a = 1;
double b = 7.35;
double c;
c = a + b;
 


   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 isn’t changed

–   a new object is created temporarily; you never really see it

–   thus, the converted value is not an lvalue

•   this isn’t an issue in the simple cases we’ve 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

Text Box: /*
 * lower: convert character to lower case
 * (ASCII only!)
 */
int lower (int c)
{
	if (c >= ‘A’ && c <= ‘Z’)
		return c + ‘a’ - ‘A’;
	else
		return c;
}
 


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

Text Box: int a, b, c;
c = a % b;
 


•      Computes the remainder of the division

•      Both operands must be integers

precedence and associativity