Expressions - cppreference.com (original) (raw)

An expression is a sequence of operators and their operands, that specifies a computation.

Expression evaluation may produce a result (e.g., evaluation of 2 + 2 produces the result 4), may generate side-effects (e.g. evaluation of printf("%d", 4) sends the character '4' to the standard output stream), and may designate objects or functions.

Contents

[edit] General

[edit] Operators

Common operators
assignment increment decrement arithmetic logical comparison member access other
a = ba += ba -= ba *= ba /= ba %= ba &= ba |= ba ^= ba <<= ba >>= b ++a --aa++a-- +a -aa + ba - ba * ba / ba % b~aa & ba | ba ^ ba << ba >> b !aa && ba | b a == ba != ba < ba > ba <= ba >= b a[b] *a &aa->ba.b

[edit] Conversions

[edit] Other

generic selections can execute different expressions depending on the types of the arguments (since C11)
Floating-point arithmetic may raise exceptions and report errors as specified in math_errhandling The standard #pragmas FENV_ACCESS, FP_CONTRACT, and CX_LIMITED_RANGE as well as the floating-point evaluation precision and rounding direction control the way floating-point arithmetic are executed. (since C99)

[edit] Primary expressions

The operands of any operator may be other expressions or they may be primary expressions (e.g. in 1 + 2 * 3, the operands of operator+ are the subexpression 2 * 3 and the primary expression 1).

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)

predefined constants true/false are values of type bool predefined constant nullptr is a value of type nullptr_t (since C23)
compound literals are values of struct, union, or array type directly embedded in program code (since C99)

[edit] Unevaluated expressions

The operands of the sizeof 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 a generic 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

[edit] See also