An expression is a sequence ofoperators and theiroperands, that specifies a computation.
Expression evaluation may produce a result (e.g., evaluation of2+2 produces the result4), may generate side-effects (e.g. evaluation ofprintf("%d",4) sends the character'4' to the standard output stream), and may designate objects or functions.
[edit]General
- value categories (lvalue, non-lvalue object, function designator) classify expressions by their values
- order of evaluation of arguments and subexpressions specifies the order in which intermediate results are obtained
[edit]Operators
[edit]Conversions
- Implicit conversions take place when types of operands do not match the expectations of operators
- Casts may be used to explicitly convert values from one type to another.
- constant expressions can be evaluated at compile time and used in compile-time context (non-VLA(since C99)array sizes, static initializers, etc)
- generic selections can execute different expressions depending on the types of the arguments
| (since C11) |
[edit]Primary expressions
The operands of any operator may be other expressions or they may beprimary expressions (e.g. in1+2*3, the operands of operator+ are the subexpression2*3 and the primary expression1).
Primary expressions are any of the following:
1) Constants and literals (e.g.2 or"Hello, world")
Any expression in parentheses is also classified as a primary expression: this guarantees that the parentheses have higher precedence than any operator.
[edit]Constants and literals
Constant values of certain types may be embedded in the source code of a C program using specialized expressions known as literals (for lvalue expressions) and constants (for non-lvalue expressions)
- integer constants are decimal, octal, or hexadecimal numbers of integer type.
- character constants are individual characters of typeint suitable for conversion to a character type or of typechar8_t,(since C23)char16_t,char32_t, or(since C11)wchar_t
- floating constants are values of typefloat,double, orlongdouble
- string literals are sequences of characters of typechar[],char8_t[](since C23),char16_t[],char32_t[],(since C11) orwchar_t[] that represent null-terminated strings
- compound literals are values of struct, union, or array type directly embedded in program code
| (since C99) |
[edit]Unevaluated expressions
The operands of thesizeof operator are expressions that are not evaluated (unless they are VLAs)(since C99). Thus,size_t n=sizeof(printf("%d",4)); does not perform console output.
The operands of the_Alignof(until C23)alignof(since C23) operator, the controlling expression of ageneric selection, and size expressions of VLAs that are operands of_Alignof(until C23)alignof(since C23) are also expressions that are not evaluated. | (since C11) |
[edit]References
- C23 standard (ISO/IEC 9899:2024):
- 6.6 Constant expressions (p: TBD)
- C17 standard (ISO/IEC 9899:2018):
- 6.5 Expressions (p: 55-75)
- 6.6 Constant expressions (p: 76-77)
- C11 standard (ISO/IEC 9899:2011):
- 6.5 Expressions (p: 76-105)
- 6.6 Constant expressions (p: 106-107)
- C99 standard (ISO/IEC 9899:1999):
- 6.5 Expressions (p: 67-94)
- 6.6 Constant expressions (p: 95-96)
- C89/C90 standard (ISO/IEC 9899:1990):
[edit]See also