C programming
Lecture III
Sorin Draghici
Wayne State University

Summary
Variables
Operators
Expressions
Statements

Variable

A variable is a memory location i.e. a place where one can store a value.
A variable has:
Variables

Example:
int x = 7 ;

type: int (integer)
value: 7
address: 100
name: x

Operators

Assignment: this operator stores a value in a given memory location

Examples:
x = 7 ;
y = x + 1 ;
y = y + 1 ;
z = y ;

Assignment

The assignment works in the following way:

Left value - Right value

When a variable is found on the left hand side of the assignment operator, the compiler will use the address of the memory location corresponding to the variable - left value

When a variable is found on the right hand side of the assignment operator, the compiler will use the content of the memory location corresponding to the variable - right value

Arithmetic operators

Addition + binary
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-- ;

Incrementing & Decrementing

The ++ and -- operators can be used before or after a variable (prefix or postfix).
If it is used before, the incrementation is done before the value of the variable is used.
If it is used after, the incrementation is done after the value of the variable is used.

Example: y = 4 ;

z = 3 + ++y ; z = 8
z = 3 + y++ ; z = 7

Precedence

Precedence refers to the order in which the operations are performed.

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.

Expressions

An expression is a combination of operators and operands.

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.

Expressions (cont.)

To find the value of the expression, one just performs the operations in the order dictated by operator precedence.
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) ;

Statements

A statement is a complete instruction to the computer. In C, statements are indicated by a semicolon at the end.

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).

Summary

Variables
Operators
Expressions
Statements

Reading

Chap. 5 (pg. 120 - 158) and 6 (160-203)
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