Lecture III

Wayne State University

Operators

Expressions

Statements

A variable has:

- a
**type**= what sort of value we store in that location

- a
**value**= the content of the memory location (can change in time)

- an
**address**= the address of the location

- a
**name**= something we use to identify it

int x = 7 ;

**type: **int (integer)

**value**: 7

**address**: 100

**name**: x

Examples:

x = 7 ;

y = x + 1 ;

y = y + 1 ;

z = y ;

- The value of the expression in the left hand side will be calculated. This value has to be an address.

- The value of the expression in the right hand side will be calculated. This value can be anything.

- The value of the right hand side will be stored at the address given by the value on the left hand side.

**
**

Subtraction - binary

Multiplication * binary

Division / binary

Sign operators +,- unary

Modulus % binary (remainder)

Increment ++ unary

Decrement -- unary

Example:

x = y + 2 ;

z = x + 3*(y+1) ;

z = z - 1 ; z-- ;

If it is used before, the incrementation is done

If it is used after, the incrementation is done

Example: y = 4 ;

z = 3 + ++y ; z = 8

z = 3 + y++ ; z = 7

Example:

y = 4 ;

z = ++y + 5 ; ++ is performed before +

we say that the precedence of ++ is higher than that of +

You have to know the precedence of the operators. If you are not sure, use parentheses.

Examples:

3

-6

a*(b+d/d)

x = y

In C, every expression has a value and the program execution is nothing but evaluating expressions.

The value of an assignment is the value of the expression on the right hand side.

It is possible to have:

x = y = z ;

which is x = (y = z) ;

Example:

x = 4 is an expression

x = 4 ; is a statement

Sequence points are points in which the side effects are evaluated. A side effect is any modification of an object (data or file).

Operators

Expressions

Statements

Homework:

Ex: 2, 3 pg. 158 (this is in addition to the homework assigned in the lab session)

Practice using the review questions:

1 - 7 pag. 156

1 - 6 pag. 116