VOOZH about

URL: https://www.geeksforgeeks.org/c/lvalue-and-rvalue-in-c-language/

⇱ lvalue and rvalue in C language - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

lvalue and rvalue in C language

Last Updated : 22 Jun, 2022

lvalue:- 

lvalue simply means an object that has an identifiable location in memory (i.e. having an address).

  • In any assignment statement "lvalue" must have the capability to store the data.
  • lvalue cannot be a function, expression (like a+b) or a constant (like 3 , 4 , etc.).

: "l-value" refers to memory location which identifies an object. l-value may appear as either left hand or right hand side of an assignment operator(=). l-value often represents as identifier. Expressions referring to modifiable locations are called "modifiable l-values". A modifiable l-value cannot have an array type, an incomplete type, or a type with the const attribute. For structures and unions to be modifiable lvalues, they must not have any members with the const attribute. The name of the identifier denotes a storage location, while the value of the variable is the value stored at that location. An identifier is a modifiable lvalue if it refers to a memory location and if its type is arithmetic, structure, union or pointer. For example, if ptr is a pointer to a storage region, then *ptr is a modifiable l-value that designates the storage region to which ptr points. In C, the concept was renamed as “locator value”, and referred to expressions that locate (designate) objects. The l-value is one of the following:

  1. The name of the variable of any type i.e. , an identifier of integral, floating, pointer, structure, or union type.
  2. A subscript ([ ]) expression that does not evaluate to an array.
  3. A unary-indirection (*) expression that does not refer to an array
  4. An l-value expression in parentheses.
  5. A const object (a nonmodifiable l-value).
  6. The result of indirection through a pointer, provided that it isn't a function pointer.
  7. The result of member access through pointer(-> or .)

r-value:-

r-value simply means, an object that has no identifiable location in memory (i.e. having an address).

  • Anything that is capable of returning a constant expression or value.
  • Expression like a+b will return some constant.

: r-value" refers to data value that is stored at some address in memory. A r-value is an expression, that can't have a value assigned to it, which means r-value can appear on right but not on left hand side of an assignment operator(=). 

Note: The unary & (address-of) operator requires an l-value as its operand. That is, &n is a valid expression only if n is an l-value. Thus, an expression such as &12 is an error. Again, 12 does not refer to an object, so it's not addressable. For instance, 

Remembering the mnemonic, that l-values can appear on the left of an assignment operator while r-values can appear on the right.

Reference: https://learn.microsoft.com/en-us/cpp/c-language/l-value-and-r-value-expressions?redirectedfrom=MSDN

Comment
Article Tags:
Article Tags: