C Language / Lesson 3 | Back to Index or to Previous Page or to Next Page |
Lvalues / The Assignment =
In C values are assigned to variables or other elements
using the = sign exactly as in most languages
Example :
int x ;
x = 15 ;
But unlike most other languages the = sign is not used
for other purposes like comparisons.
For instance in Basic A=B is
an assignment but IF A=B is a comparison
(A and B are compared for equality)
In C
a. if we want to compare a
and b , then the condition will look like this
if
( a == b ) ....
b. if on purpose or accidentaly
we write
if
( a = b ) ...
then b will be assigned to a and the truth of the
condition will be the value of a.
(Remember that a number obtained as a logical expression is
always true unless it is =0 )
Although there is a mysterious confusion in several C
language books regarding the definition of an lvalue
we can define it easily with two similar statements.
a. an expression which is allowed to appear at the
left side of an assignment = , is an lvalue
b. an expression describing somewhere where we
can store a value is an lvalue .
Both definitions are trying to say the same thing, since a single = sign in C always means an assignment.
Some reasons why the lvalue definition is important in C :
FIRST : The address operator ( & ) gives the ability
for expressing addresses in addition to values. But an
address cannot change. You can change the value at an
address in memory but the address is not
modifiable. Thus if we attempt to add the following line in a C program
&x = 7 ;
where x is a variable previously defined in the program
then we will get a compilation error : Not an lvalue
Of course &x is not somewhere to assign a value.
If we wanted to assign a value to x then
x = 7
;
is what we need.
SECOND : Aggregates (arrays) in C are composite types i.e. they don't receive
values with a simple assignment (see lesson 1).
Let's mention the case of strings which can be assigned
a value with the use of the strcpy( ) function
example
strcpy
( name , "George") ;
An aggregate's name in a statement means the address of
the aggregate and not its value. That's why
a statement like this
name = "George"
;
where name is defined as a character aggregate (string) will cause a
compilation error : Not an lvalue
What else ?
Although in traditional C the lvalue issue is raised usually in cases similar to the above 'FIRST' example i.e. when the programmer accidentally attempts to store to destination that is an address, which is logically impossible, a few more cases of Not lvalues will be presented here :
Constants are NOT lvalues. This is of course obvious : A constant is by definition something that doesn't change. Trying to assign a value to the constant means "change the constant". That is , the computer is instructed to change an expression that can't change. Do you want to try it? Then enter this statement in your program :
int |
x; 5 = x ; |
Of course this is obvious. Whenever I ask an inexperienced young
programmer with no knowledge of C language to discuss this, I always get the
answer "this is wrong!". But then, when I ask about this :
int |
x; if (5 = x) .... |
most people say "this is OK !". Well it's not OK in C , because this condition doesn't compare 5 and x for equality. Instead due to the assignment operator it attempts to assign x to 5 which as explained is crazy.
More composite cases :
The advantage of C from the usage of two different operators ( the = for assignments and the == for comparisons) allows for sophisticated code like this
|
This statement 'says' : Assign the logical result of the comparison 'y==5' to
x and then use it as a condition (i.e. if it is true proceed to the execution
of the flow control block that follows) . The reason it is inetrpreted like
that is that the = has the lowest priority from all operators and so the ==
must be executed first.
The above style of a condition is legal but not really useful. There is a slight variation though, which is really useful and very popular to C programmers
|
The paranthesis around the x=y makes all the difference. Now the priority goes to the = operator and the statement says : "Assign the value of y to x and then compare x and 5 for equality". In other words we have a 2 in 1 statement since in another language this would be done in two steps
x=y; if (x==5) etc ...
Back to Index or to Previous Page or to Next Page