Reputation: 25
I assume when we declare int a ; a=10;
, then the compiler takes r-value & put it in it's l-value. At that time the l-value always treated as a address in the memory location i.e. a or any other variable in assignment operation at the left hand side treated as (&variable)
. So we are able to catch its address to put the r-value in it.
Is my assumption is right or wrong?If wrong, then what is basic idea behind it?
Upvotes: 0
Views: 121
Reputation: 263237
An assignment operator has two operands. The left operand must be a "modifiable lvalue".
An "lvalue" is, roughly, an expression that designates an object.
(An "rvalue", as the C standard uses the term, is simply the value that results from evaluating an expression. The C standard defines the term, but rarely uses it. The usage is a bit inconsistent: an "lvalue" is an expression, but an "rvalue" is a value.)
The right operand is evaluated to yield a value. That value is then converted, if necessary, to the type of the left operand, and the result is stored in the object designated by the left operand.
Addresses are not required. For example, a variable defined with register
has no address, but it can still be the target of an assignment.
If you want the gory details, see section 6.5.16 of the C standard (the link is to n1570.pdf
, the most recent draft, a 1.7-megabyte PDF).
Upvotes: 1
Reputation: 123448
You're making it more complicated than it needs to be. The language definition simply states:
An assignment operator stores a value in the object designated by the left operand.
(6.5.16, para 3).
The only general constraint is that the left operand be a modifiable lvalue. An lvalue can correspond to a register (which has no address) or an addressable memory location. Exactly how it happens is an implementation detail.
Upvotes: 2
Reputation: 47699
The terms "lvalue" and "rvalue" come from being on the left and right of the =
symbol and hence are self-reflexive definitions. (Ie, not real helpful.)
But the important point is that an rvalue must be able to produce a value (ie, finite pattern of bits), while an lvalue must be capable of being having assigned to it a similar value.
As such, an lvalue must ultimately represent a storage address or register or I/O port or something else (is there anything else?) that can be "written to". But this does not mean that the lvalue contains an address -- it merely represents the addressable entity in the compiler, and will be translated to "real" addressing info in the generated assembly. Ie, it's simply a name for something.
An rvalue, on the other hand, need not represent a "real thing", but can be a literal or some expression or call or whatever, so long as it produces a "value" when asked.
Upvotes: 0