Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Operators and Expressions

Lvalues and rvalues in C

Reference for lvalues and rvalues in C: which expressions designate objects, modifiable lvalues, address-of rules, and the errors you get otherwise.

An lvalue is an expression that designates an object, a place in memory with a type. Variables, array elements, struct members and the result of dereferencing a pointer are lvalues. An rvalue is any other expression value: a constant, the result of arithmetic, a function's return value. It is a value to compute with, not a place to store into.

The distinction explains the left-hand side of an assignment, what & can be applied to, and what ++ can modify. The name comes from "left value": an lvalue can appear on the left of =, though not every lvalue can be assigned to.

C
int x = 3;
x = 5;           /* x is a modifiable lvalue */
5 = x;           /* error: 5 is an rvalue */
int *p = &x;     /* & needs an lvalue */

Try it

C Expressions that are lvalues

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Modifiable or not

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

An lvalue is a property of an expression, not a syntax. The main cases:

Expression Lvalue?
Identifier of an object (x) Yes (modifiable unless const or an array)
a[i], *p, p->m, s.m (when s is an lvalue) Yes
A string literal ("abc") Yes (it designates an array object), but you must not modify it
A compound literal (int){5} Yes
x + 1, -x, a constant, sizeof x No
f() (a call), s.m where s is a function result No
x++, ++x, x = y, (x, y), c ? a : b No (in C; some are lvalues in C++)
A cast (int)x No

A modifiable lvalue is an lvalue that is not an array, has a complete type, is not const-qualified, and (for structs and unions) has no const-qualified member.

Errors: assigning to a non-lvalue gives "lvalue required as left operand of assignment"; assigning to a const object gives "assignment of read-only variable". Taking & of a non-lvalue gives "lvalue required as unary '&' operand". Writing to a string literal's characters compiles but is undefined behavior at run time.

Usage notes

  • Where lvalues are needed: the left side of = and compound assignments, the operand of ++ and --, and the operand of &.
  • Lvalue conversion. When an lvalue is used for its value (y = x) the object is read and the result is an rvalue. Used with &, sizeof or on the left of =, no such conversion happens.
  • Arrays are lvalues but not modifiable; use memcpy, a loop or wrap the array in a struct. A struct containing an array can be copied by =.
  • Functions are not objects; a function designator is neither lvalue nor rvalue in the strict sense, but &f is valid.
  • Register objects cannot have their address taken (register int r; &r is an error).
  • Bit-fields can be assigned but have no address, so &s.bits is an error.
  • Function results are not lvalues, so f() = 3 and &f() do not compile. A pointer returned by a function can be dereferenced to get an lvalue: *get_ptr() = 3.
  • C++ differs: c ? a : b, ++x and x = y can be lvalues in C++. In C, take a pointer if you need that.