Javatpoint Logo
Javatpoint Logo

Lvalue and Rvalue in C

In this article, we will discuss the lvalue and rvalue in C with their examples.

What is lvalue?

Lvalue merely denotes a memory location-identifiable item (i.e., one with an address). Any assignment statement must allow for the storage of data in "lvalue". A function, an expression (such as a+b), or a constant (such as 3, 4, etc.) cannot be a lvalue.

The term "l-value" describes a memory location that is used to identify an object. The left or right side of the assignment operator (=) may contain a l-value. L-value is frequently used as identification. "Modifiable l-values" are expressions that refer to customizable places. An array type, an incomplete type, or a type with the const attribute is all prohibited for a changeable l-value. There cannot be any members with the const attribute in structures or unions in order for them to be editable lvalues.

The value of the variable is the value that is stored at the location designated by the name of the identifier. If an identifier relates to a memory location and its type is arithmetic, structure, union, or pointer, it qualifies as a changeable lvalue. For instance, *ptr is a changeable l-value that identifies the storage region to which ptr points if ptr is a pointer to a storage region. Expressions that locate (designate) objects were given the new moniker of "locator value" in C. One of the following describes the l-value:

  • Any variable's name, such as an identifier for an integral, floating-point, pointer, structure, or union type.
  • An expression for the subscript ([]) that does not evaluate an array.
  • An expression with a unary indirection (*) that doesn't make reference to an array
  • An enclosed l-value
  • A const object (an l-value that cannot be changed).
  • Indirection through a pointer that, if it's not a function pointer, produces the desired effect.
  • Member access by pointer(-> or.) results in.



The value of 'a' is 1
The value of 'b' is 1


In this example, a and b are declared as integer variables. A value of 1 is given to a, and then b is given that same value. The result will indicate that both a and b have values of 1.

Because a l-value is anticipated on the left side of the assignment operator (=), the line 9 = a; will result in a compilation error. In this example, it is not an acceptable l-value since the literal 9 cannot be given a new value.

What is r-value?

R-value simply refers to an object without an address or a location that can be found in memory. It can produce a constant expression or value as a return value. A simple expression like a+b will yield a constant.

The term "r-value" describes a data item that is kept in memory at a specific address. A r-value is an expression that cannot have a value assigned to it. Hence it can only occur on the right side of the assignment operator (=), not the left.



The value of 'a' is 1
The value of 'b' is 0
The value at index 12 of 'arr' is 42
The value of 'obj.m' is 24


In this example, the variables a, b, p, q, arr, obj, and ptr are all declared. There are several examples of assignments and operations involving l- and r-values.

The output will display 1, 0, 42, and 24, respectively, for the values of a, b, the element at index 12 of arr, and the member m of obj.

Note: An l-value is needed as the operand for the unary & (address-of) operator. In other words, &n is only a legitimate expression if n is a l-value. Therefore, an expression like &12 is incorrect. Once more, 12 is not addressable because it does not refer to an object.

For example,


The value of 'a' is undefined (garbage value)
The value of 'x' is undefined (garbage value)
The value of 'y' is undefined (garbage value)


In this example, a, p, x, and y are declared as variables. The assignment operator (=) is used to provide p access to the address of a. However, because the address of a (&a) is an r-value and cannot be given a new value, the line &a = p; will result in a compilation error.

The ternary expression is used in the line (x< y? y: x) = 0. The outcome of the ternary expression is a l-value, which enables us to give either y or x a value of 0 depending on the statement (x y).

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA