Value categories
Each expression in C (an operator with its arguments, a function call, a constant, a variable name, etc) is characterized by two independent properties: a type and a value category.
Every expression belongs to one of three primary categories: lvalues, function designators, and non-lvalue object expressions (rvalues).
Contents |
[edit] Lvalue expressions
Lvalue expression is any expression with object type other than the type void
, which potentially designates an object (the behavior is undefined if an lvalue does not actually designate an object when it is evaluated). In other words, lvalue expression evaluates to the object identity.
Lvalue expressions can be used in the following lvalue contexts:
- as the operand of the address-of operator (except if the lvalue designates a bit field or was declared register).
- as the operand of the pre/post increment and decrement operators.
- as the left-hand operand of the member access (dot) operator.
- as the left-hand operand of the assignment and compound assignment operators.
If an lvalue expression is used in any context other than sizeof, _Alignof, or the operators listed above, non-array lvalues of any complete type undergo lvalue conversion, which models the memory load of the value of the object from its location. Similarly, array lvalues undergo array-to-pointer conversion when used in any context other than sizeof
, _Alignof
, address-of operator, or array initialization from a string literal.
The semantics of const/volatile/restrict-qualifiers and atomic types apply to lvalues only (lvalue conversion strips the qualifiers and removes atomicity).
The following expressions are lvalues:
- identifiers, including function parameters, provided they were declared as designating objects (and not functions)
- string literals
- (C99) compound literals
- the result of a member access (dot) operator if its left-hand argument is lvalue
- the result of a member access through pointer
->
operator - the result of the indirection (unary
*
) operator applied to a pointer to object - the result of the subscription operator (
[]
)
[edit] Modifiable lvalue expressions
A modifiable lvalue is any lvalue expression of complete, non-array type which is not const-qualified, and, if it's a struct/union, has no members that are const-qualified, recursively.
Only modifiable lvalue expressions may be used as arguments to increment/decrement, and as left-hand arguments of assignment and compound assignment operators.
[edit] Non-lvalue object expressions
Colloquially known as rvalues, non-lvalue expressions are the expressions of object types that do not designate objects, but rather values that have no object identity or storage location. The address of a non-lvalue expression cannot be taken.
The following expressions are non-lvalue expressions:
- integer, character, and floating constants
- all operators not specified to return lvalues, including
- any function call expression
- any cast expression (note that compound literals, which look similar, are lvalues)
- member access operator (dot) applied to a non-lvalue structure/union, f().x, (x,s1).a, s1=s2).m
- all arithmetic, relational, logical, and bitwise operators
- increment and decrement operators (note: pre- forms are lvalues in C++)
- assignment and compound assignment operators (note: they are lvalues in C++)
- the conditional operator (note: may be lvalue in C++)
- the comma operator (note: may be lvalue in C++)
- the address-of operator, even it if is neutralized by being applied to the result of the unary
*
operator
As a special case, expressions of type void
are assumed to be non-lvalue object expressions that yield a value which has no representation and requires no storage.
Note that a struct/union rvalue that has a member (possiblly nested) of array type does in fact designate an object with temporary lifetime. This object can be accessed through lvalue expressions that form by indexing the array member or by indirection through the pointer obtained by array-to-pointer conversion of the array member.
[edit] Function designator expression
A function designator (the identifier introduced by a function declaration) is an expression of function type. When used in any context other than the address-of operator, sizeof, and _Alignof (the last two generate compile errors when applied to functions), the function designator is always converted to a non-lvalue pointer to function. Note that the function-call operator is defined for pointers to functions and not for function designators themselves.
[edit] See also
C++ documentation for Value categories
|