Another useful set of elementary operators are the assignment operators. The simplest of these is "=". Unlike the operators we have seen so far the choice of first operand is restricted to the name of a variable. A typical example would be
x=7 The value of this expression is 7 but as a side effect the value 7 is stored in the variable x. The side effect is often more important than the value of the expression. Again a programming example is in order.
main()
{
int x=4;
printf("The value of x is %d\n",x);
printf("The value of \"x=8\" is %d\n",x=8);
printf("The value of x is %d\n",x);
}
resulting in the output The value of x is 4 The value of "x=8" is 8 The value of x is 8Another example underlines the point
main()
{
int x=2,y=3;
printf("x = %d, y = %d\n",x,y);
printf("Value of \"(y+(x=8))\" is %d\n",
y+(x=8));
printf("x = %d, y = %d\n",x,y);
}
producing the output x = 2, y = 3 Value of "(y+(x=8))" is 11 x = 8, y = 3These examples might look a little odd to those familiar with other programming languages who might be happier with the following program.
main()
{
int x=4;
printf("The value of x is %d\n",x);
x=8;
printf("The value of x is %d\n",x);
}
which produced exactly the same first and last lines of output. The line between the two
printf()
function calls is a single statement consisting of the expression x=8 The final semi-colon is necessary to ensure that this line is a statement. There are several good reasons, mainly concerned with keeping compiler design simple, for treating "=" as an operator rather than treating assignment as a special sort of statement, the technique adopted by many other programming languages.
Technically the thing that appears at the left hand side of an assignment expression must be an lvalue , this means the address of a memory location in which a value can be stored. In the simple assignment expression
x=x+1 the "x" on the left hand side represents the address of a memory location whereas the "x" on the right hand side of the "=" symbol represents the value stored in the location with the address "x". This confusion is common to nearly all programming languages.
The expression
x=x+1 repays further examination. This expression includes the two operators "=" and "+" and it is proper to ask whether it means
evaluate the expression x=x add 1 to the result of the first evaluationor
evaluate the expression x+1 assign the result to xWe have already seen that problems of this nature can be resolved by considering the relative precedence of the operators. The ANSI standard states that all the assignment operators have a lower priority or precedence than any other operator except the rather bizarre comma operator which we won't meet for some time. This means that the second interpretation of "x=x+1" is the correct interpretation.
The value of an assignment expression may, of course, be assigned to a variable. This leads to constructs such as
x = y = z = p+3 Unlike the arithmetic operators discussed earlier assignment operators associate or group right to left. This means that, in the above example, the first step is the evaluation of the expression p+3 followed by assignment of its value to z. The value of the expression
z = p+3 is then assigned to y and so on.
There are several more assignment operators that provide a short-hand way of writing
variable = variable operator expression The following are based on the arithmetic operators we have discussed so far
+= -= *= /= %= There are others which will be discussed later in the course. The economy of writing
count_so_far += input_value compared with
count_so_far = count_so_far + input_value is obvious and these assignment operators are widely used by C programmers. In all cases the value of the assignment expression is the value assigned to variable whose name is the first operand of the expression. The following program exercises the assignment operators. Note the "%%" in the penultimate output printf() format.
main()
{
int x=4;
int n1=2,n2=1,n3=4,n4=5,n5=3;
printf("Initial value of x is %d\n",x);
printf("Value of x += %d is %d\n",n1,x+=n1);
printf("Value of x -= %d is %d\n",n2,x-=n2);
printf("Value of x *= %d is %d\n",n3,x*=n3);
printf("Value of x /= %d is %d\n",n4,x/=n4);
printf("Value of x %%= %d is %d\n",n5,x%=n5);
printf("Final value of x is %d\n",x);
}
The output is shown below. It is an interesting exercise to try and follow through the execution of the program and predict the values that are printed out. Initial value of x is 4 Value of x += 2 is 6 Value of x -= 1 is 5 Value of x *= 4 is 20 Value of x /= 5 is 4 Value of x %= 3 is 1 Final value of x is 1If you peeked, try changing the initial values, predicting the results and then try it on a computer.
If you couldn't figure out what was going on in the previous example let's go through it step by step.
When the second printf() is executed it is necessary to calculate the value of "x+=n1", since n1 has the value 2 and x has the value 4 the value of "x+n1" is 6 and remembering that
x+=n1 is equivalent to
x = x + n1 it is now clear that x takes the value 6 and that this is also the value of the assignment expression.
On the next line the value of "x-=n2" is required. As n2 has the value 1 and x was changed to 6 on the previous line the value of this is 5 which value is assigned to x.
On the fourth printf() line the value of x*=n3 is required, since n3 is 4 and x is now 5 this is 20. On the fifth line is value of "x/=n4" is the same as "x=x/n4" which is 4.
Finally "x%=n5" is calculated, remembering that x is now 4 and n5 is 3 and that the expression is equivalent to "x=x%n5", it is easy to see that the value is 1, the remainder when 4 is divided by 3.