Previous: 2.2.3 Variable Declarations
Up: 2.2 Organization of C Programs - Simple Statements
Next: 2.2.5 Generating Output
Previous Page: 2.2.3 Variable Declarations
Next Page: 2.2.5 Generating Output

2.2.4 The Assignment Statement

The next three statements in our program assign initial values to variables, i.e. store initial values into objects represented by the variables. The assignment operator is =.

id_number = 123;
     hours_worked = 20.0;
     rate_of_pay = 7.5;

Each of the above statements stores the value of the expression on the right hand side of the assignment operator into the object referenced by the variable on the left hand side, e.g. the value stored in id_number is 123 (Figure 2.3). We will say the (current) value of id_number is 123. The value of a variable may change in the course of a program execution; for example, a new assignment can store new data into a variable. Storing new data overwrites the old data; otherwise, the value of a variable remains unchanged.

The ``right hand side'' of these three assignments is quite simple, a decimal constant. (The compiler will take care of converting the decimal number we use in the source code into its appropriate binary representation). However, in general the right hand side of an assignment may be an arbitrary consisting of constants, variable names and arithmetic operators (functions may also occur within expressions). For example, next, we calculate the product of the value of hours_worked and the value of , and assign the result to the variable pay. The multiplication operator is *.

pay = hours_worked * rate_of_pay;
The semantics of the assignment operator is as follows: the expression on the right hand side of the assignment operator is first evaluated by replacing each instance of a variable by its current value and the operators are then applied to the resulting operands. Thus, the above right hand side expression is evaluated as:
20.0 * 7.5

The resulting value of the expression on the right hand side is then assigned to the variable on the left hand side of the assignment operator. Thus, the value of 20.0 * 7.5, i.e. 150.0, is stored in pay (Figure 2.4).

The above assignment expression may be paraphrased in English as follows:

``SET pay TO THE VALUE OF hours_worked * rate_of_pay''
or,
``ASSIGN TO pay THE VALUE OF hours_worked * rate_of_pay''
The syntax of an assignment statement is:

The class of items allowed on the left hand side of an assignment operator is called an Lvalue, a mnemonic for left value. Of course, <Lvalue> must always reference an object where a value is to be stored. In what we've see so far, only a variable name can be an <Lvalue>. Later we will see other ways of referencing an object which can be used as an <Lvalue>.

As we can see from the above discussion, variables provide us a means for accessing information in our program. Using a variable on the left hand side of an assignment operator allows us to store a value in its memory cell. Variables appearing elsewhere in expressions cause the current value of the data item to be read and used in the expression.

In C every expression evaluated during execution results in a value. Assignment is also an expression, therefore also results in a value. Assignment expressions may be used anywhere expressions are allowed. The rule for evaluating an assignment expression is: evaluate the expression on the right hand side of the assignment operator, and assign the value to the variable on the left hand side. The value of the entire assignment expression is the value assigned to the left hand side variable. For example, x = 20 assigns 20 to x, and the value of the entire assignment expression is 20. So if we wrote y = x = 20, the variable y would be assigned the value of the expression x = 20, namely 20. In our programming example we have used assignment expressions as statements but ignored their values.

Any expression terminated by a semi-colon is a statement. Of course, a statement is typically written to perform some useful action. Some additional examples of expressions as statements are:

20;
     5 + 10;
     z = 20 * 5 + 10;
     ;
The last statement is an empty statement which does nothing. The expressions in the first two statements accomplish nothing since nothing is done with their values.

C has a rich set of operators for performing computations in expressions. The common arithmetic operators and their meanings are shown in Table 2.1. Two types of operators are shown; unary operators which take one operand, and binary operators which take two operands. The unary operators, + and - affect the sign of the operand. The binary operators are those you are familiar with, except possibly %. This is the mod operator, which we will describe below, but first one other point to make is that for the division operator /, if both operands are type integer, then integer division is performed, discarding and fractional part with the result also being type integer. Otherwise, a floating point result is produced for division. The mod operator evaluates to the remainder after integer division. Specifically, the following equality holds: In words, if x and y are integers, multiplying the result of integer division by the denominator and adding the result of mod produces the numerator. We will see many more operators in future chapters.



Previous: 2.2.3 Variable Declarations
Up: 2.2 Organization of C Programs - Simple Statements
Next: 2.2.5 Generating Output
Previous Page: 2.2.3 Variable Declarations
Next Page: 2.2.5 Generating Output

tep@wiliki.eng.hawaii.edu
Tue Aug 16 14:01:55 HST 1994