Previous | Table of Contents | Next |
Assignments in C are not distinguished statements. They are ordinary expressions; the assignment operator = is an ordinary binary operator. (This is in keeping with Cs emphasis on expressions.) Placed between two subexpressions, the assignment operator = assigns the value of the right-hand subexpression to the location named by the left. For example,
a = b + 1
evaluates the subexpression b + 1 and assigns it to the variable a.
Note that although a is the name of a variable, here its value is not fetched. Because it appears on the left-hand side of an assignment operator, it serves as the destination for a value, not a source.
Like any other expression, an assignment expression has a value. If an assignment expression appears as a subexpression within some larger expression, the value passed on to the surrounding expression is simply the value that was assigned to the left-hand side. For example, the expression
f(a = b + 1)
assigns the value b + 1 to a and then passes that same value as an argument to the function f.
An assignment expression is an example of an expression with a side effect. Besides computing a value, an assignment expression also causes a change to the state of the program, by virtue of the fact that it modifies a variable by assigning a new value to it.
The assignment operator has very low precedence, so an expression like
a = b + 1
is interpreted as
a = (b + 1)
which is certainly what was meant. The assignment operator associates (groups) from right to left, so an expression like
a = b = 0
is interpreted as
a = (b = 0)
and therefore sets a to the same value it sets b. (An expression like a = b = 0 is a common, compact idiom for assigning the same value to several variables at once.)
Obviously, the left-hand side of an assignment expression must represent a value that can be assigned to. It may be an ordinary variable, or an element of an array, or certain other location-designating expressions. But it may not be the name of a variable that was declared const, or an entire array, or an expression that only computes a value. (As a counterexample, the expression a + 1 = b is meaningless.)
As in most computer languages, the assignment operator indicates an active assignment of a value to a location; it does not represent a test for or assertion of equality. (Some languages use notations like := for assignment, in order to make this distinction more evident.) The expression
i = i + 1
is a meaningful and extremely common idiom (though not as commonly written in this form in C). The interpretation is as before: The expression i + 1 is evaluated, and the result assigned to i, giving it a value one greater than it had before.
C provides the usual set of operators for performing relational comparisons and logical or boolean operations. These operations are once again performed by ordinary operators in the context of ordinary expressions. The relational operators are as follow:
Operator | Description |
---|---|
< | less than |
<= | less than or equal to |
> | greater than |
>= | greater than or equal to |
== | equal to |
!= | not equal to |
These are all binary operators, comparing their two operands and returning a true or false result. They can be used not only with numerical quantities, but also with pointers, as discussed in section 3.6.3. (Also, we may again note that the notations <=, >=, and != are used at least in part because few keyboards or common character sets contain the symbols [le], [ge], and [ne].)
A trivial example is
a == b
which has the value true if a and b are equal, and false otherwise.
It is important to notice that the equality-testing operator is ==, not one = by itself! Accidentally writing something like
if(a = b)
is a common error, which most certainly does not test whether a and b are equal, but rather sets them to be equal. (The condition tested would then be whether the value assigned was true or false.)
True and false values can be combined, using the logical or boolean operators, which are as follow:
Operator | Description |
---|---|
&& | logical AND |
|| | logical OR |
! | logical NOT (unary) |
For example, the expression
a == 2 || a == 3
represents (or, stated another way, tests the truth or falsehood of) the condition a is equal to 2 or a is equal to 3. The expression
x > 0 && x < 10
tests whether x is between 0 and 10 (representing the condition that a mathematician would express as 0 < x < 10, although this form of expression is not meaningful in C).
Previous | Table of Contents | Next |