Value categories
Values, such as variables and literals, do not just have a data type (e.g. char
, float
, int
) but they also belong to a value category. In C, there are two value categories, l-values and r-values. The names of the categories are derived from the egocentric coordinates left and right, as l-values often appear on the left side of expressions containing an assignment operator and r-values exclusively appear on the right side of such expressions. A more accurate way to explain these categories is that l-values can be assigned to, at least once, as they have an addressable location in memory but r-values can not be assigned to as they do not have an addressable location in memory and tend to be temporary. l-values can additionally be classified as modifiable and nonmodifiable.
l-values
l-values have an addressable location in memory and can be assigned to at least once.
Modifiable
Modifiable l-values can be assigned to freely.
- non-
const
variables int myVariable = 5;
The variable (myVariable
) is a modifiable l-value but the literal (5) is an r-value.
Nonmodifiable
Nonmodifiable l-values can only be assigned to once.
- arrays
int myArray[5];
The array (myArray
) is a nonmodifiable l-value.const
variablesconst int myVariable = 5;
Theconst
variable (myVariable
) is a nonmodifiable l-value but the literal (5) is an r-value.
r-values
r-values do not have an addressable location in memory and can not be assigned to.
- functions
int myFunction();
The function (myFunction
) is an r-value.- literals
int myVariable = 5;
The literal (5) is an r-value but the variable (myVariable
) is a modifiable l-value. String literals, which are essentiallychar
arrays, are nonmodifiable l-values.