Namespaces
Variants
Views
Actions

Difference between revisions of "c/language/expressions"

From cppreference.com
< c‎ | language
m (Undo revision 75834 by Newatthis (talk) already explained two lines lower)
(N2653)
 
(13 intermediate revisions by 4 users not shown)
Line 6: Line 6:
  
 
====General====
 
====General====
* {{rlp|value_category|value categories}} (lvalue, non-lvalue, function designator) classify expressions by their values
+
* {{rlp|value_category|value categories}} (lvalue, non-lvalue object, function designator) classify expressions by their values
 
* {{rlp|eval_order|order of evaluation}} of arguments and subexpressions specifies the order in which intermediate results are obtained
 
* {{rlp|eval_order|order of evaluation}} of arguments and subexpressions specifies the order in which intermediate results are obtained
  
 
===Operators===
 
===Operators===
 
 
{{c/language/operators}}
 
{{c/language/operators}}
  
Line 21: Line 20:
  
 
====Other====
 
====Other====
* {{rlp|constant expression}}s can be evaluated at compile time and used in compile-time context (non-VLA array sizes, static initializers, etc)
+
* {{rlp|constant expression}}s can be evaluated at compile time and used in compile-time context ({{rev inl|since=c99|non-VLA }}array sizes, static initializers, etc)
 +
{{rrev|since=c11|
 
* {{rlp|generic|generic selections}} can execute different expressions depending on the types of the arguments
 
* {{rlp|generic|generic selections}} can execute different expressions depending on the types of the arguments
* Floating-point expressions may raise exceptions and report errors as specified in [[c/numeric/math/math_errhandling|math_errhandling]]
+
}}
 +
{{rrev|since=c99|
 +
* Floating-point expressions may raise exceptions and report errors as specified in {{ltt|c/numeric/math/math_errhandling}}
 
* The standard [[c/preprocessor/impl|#pragma]]s {{tt|FENV_ACCESS}}, {{tt|FP_CONTRACT}}, and {{tt|CX_LIMITED_RANGE}} as well as the [[c/types/limits/FLT_EVAL_METHOD|floating-point evaluation precision]] and [[c/numeric/fenv/FE_round|rounding direction]] control the way floating-point expressions are executed.
 
* The standard [[c/preprocessor/impl|#pragma]]s {{tt|FENV_ACCESS}}, {{tt|FP_CONTRACT}}, and {{tt|CX_LIMITED_RANGE}} as well as the [[c/types/limits/FLT_EVAL_METHOD|floating-point evaluation precision]] and [[c/numeric/fenv/FE_round|rounding direction]] control the way floating-point expressions are executed.
 +
}}
  
 
===Primary expressions===
 
===Primary expressions===
Line 32: Line 35:
 
@1@ Constants and literals (e.g. {{c|2}} or {{c|"Hello, world"}})
 
@1@ Constants and literals (e.g. {{c|2}} or {{c|"Hello, world"}})
 
@2@ Suitably declared {{rlp|identifier}}s (e.g. {{c|n}} or {{c|printf}})
 
@2@ Suitably declared {{rlp|identifier}}s (e.g. {{c|n}} or {{c|printf}})
 +
 +
{{rrev|since=c11|
 
@3@ {{rlp|generic|Generic selections}}
 
@3@ {{rlp|generic|Generic selections}}
 +
}}
  
 
Any expression in parentheses is also classified as a primary expression: this guarantees that the parentheses have higher precedence than any operator.
 
Any expression in parentheses is also classified as a primary expression: this guarantees that the parentheses have higher precedence than any operator.
Line 40: Line 46:
  
 
* {{rlp|integer constant}}s are decimal, octal, or hexadecimal numbers of integer type.
 
* {{rlp|integer constant}}s are decimal, octal, or hexadecimal numbers of integer type.
* {{rlp|character constant}}s are individual characters of type int suitable for conversion to a character type or of type {{rev inl|since=c11|{{c|char16_t}}, {{c|char32_t}}, }}or {{c|wchar_t}}
+
* {{rlp|character constant}}s are individual characters of type {{c|int}} suitable for conversion to a character type or of type {{rev inl|since=c23|{{c|char8_t}},}} {{rev inl|since=c11|{{c|char16_t}}, {{c|char32_t}}, or }}{{c|wchar_t}}
 
* {{rlp|floating constant}}s are values of type {{c|float}}, {{c|double}}, or {{c|long double}}
 
* {{rlp|floating constant}}s are values of type {{c|float}}, {{c|double}}, or {{c|long double}}
* {{rlp|string literal}}s are sequences of characters of type {{c|char[]}}, {{c|char16_t[]}}, {{c|char32_t[]}}, or {{c|wchar_t[]}} that represent null-terminated strings
+
{{rrev|since=c23|
 +
* predefined constants {{rlp|bool_constant|{{tt|true}}/{{tt|false}}}} are values of type {{c|bool}}
 +
* predefined constant {{rlpt|nullptr}} is a value of type {{lc|nullptr_t}}
 +
}}
 +
* {{rlp|string literal}}s are sequences of characters of type {{c|char[]}}{{rev inl|since=c23|, {{c|char8_t[]}}}}{{rev inl|since=c11|, {{c|char16_t[]}}, {{c|char32_t[]}},}} or {{c|wchar_t[]}} that represent null-terminated strings
 +
{{rrev|since=c99|
 
* {{rlp|compound literal}}s are values of struct, union, or array type directly embedded in program code
 
* {{rlp|compound literal}}s are values of struct, union, or array type directly embedded in program code
 +
}}
  
 
===Unevaluated expressions===
 
===Unevaluated expressions===
The operands of the {{rlp|sizeof|sizeof operator}} {{rev inl|since=c11|, the {{rlp|_Alignof|_Alignof operator}}, and the controlling expression of a {{rlp|generic|generic selection}},}} are expressions that are not evaluated{{rev inl|since=c99| (unless they are VLAs)}}. Thus, {{c|size_t n {{=}} sizeof(printf("%d", 4));}} does not perform console output.
+
The operands of the {{rlp|sizeof|{{tt|sizeof}} operator}} are expressions that are not evaluated{{rev inl|since=c99| (unless they are VLAs)}}. Thus, {{c|size_t n {{=}} sizeof(printf("%d", 4));}} does not perform console output.
 +
 
 +
{{rrev|since=c11|
 +
The operands of the {{rlp|_Alignof|{{tt|_Alignof}} operator}}, the controlling expression of a {{rlp|generic|generic selection}}, and size expressions of VLAs that are operands of {{tt|_Alignof}}<!-- DR 494 --> are also expressions that are not evaluated.
 +
}}
 +
 
 +
===References===
 +
{{ref std c17}}
 +
{{ref std | section=6.5 | title=Expressions | p=55-75}}
 +
{{ref std | section=6.6 | title=Constant expressions | p=76-77}}
 +
{{ref std end}}
 +
{{ref std c11}}
 +
{{ref std | section=6.5 | title=Expressions | p=76-105}}
 +
{{ref std | section=6.6 | title=Constant expressions | p=106-107}}
 +
{{ref std end}}
 +
{{ref std c99}}
 +
{{ref std | section=6.5 | title=Expressions | p=67-94}}
 +
{{ref std | section=6.6 | title=Constant expressions | p=95-96}}
 +
{{ref std end}}
 +
{{ref std c89}}
 +
{{ref std | section=3.3 | title=EXPRESSIONS}}
 +
{{ref std | section=3.4 | title=CONSTANT EXPRESSIONS}}
 +
{{ref std end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc see cpp | cpp/language/expressions | Expressions}}
+
{{dsc see cpp | cpp/language/expressions | Expressions | nomono=true}}
 
{{dsc end}}
 
{{dsc end}}
  
[[ar:c/language/expressions]]
+
{{langlinks|ar|cs|de|es|fr|it|ja|ko|pl|pt|ru|tr|zh}}
[[cs:c/language/expressions]]
+
[[de:c/language/expressions]]
+
[[es:c/language/expressions]]
+
[[fr:c/language/expressions]]
+
[[it:c/language/expressions]]
+
[[ja:c/language/expressions]]
+
[[ko:c/language/expressions]]
+
[[pl:c/language/expressions]]
+
[[pt:c/language/expressions]]
+
[[ru:c/language/expressions]]
+
[[tr:c/language/expressions]]
+
[[zh:c/language/expressions]]
+

Latest revision as of 12:52, 13 January 2023

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

  • 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

Common operators
assignment increment
decrement
arithmetic logical comparison member
access
other

a = b
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b

a[b]
*a
&a
a->b
a.b

a(...)
a, b
(type) a
a ? b : c
sizeof


_Alignof
(since C11)

[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.

[edit] Other

  • 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)
(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")
2) Suitably declared identifiers (e.g. n or printf)
(since C11)

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 type int suitable for conversion to a character type or of type char8_t,(since C23) char16_t, char32_t, or (since C11)wchar_t
  • floating constants are values of type float, double, or long double
(since C23)
  • string literals are sequences of characters of type char[], char8_t[](since C23), char16_t[], char32_t[],(since C11) or wchar_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 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 operator, the controlling expression of a generic selection, and size expressions of VLAs that are operands of _Alignof are also expressions that are not evaluated.

(since C11)

[edit] References

  • 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):
  • 3.3 EXPRESSIONS
  • 3.4 CONSTANT EXPRESSIONS

[edit] See also

C++ documentation for Expressions