Namespaces
Variants
Views
Actions

Difference between revisions of "c/language/expressions"

From cppreference.com
< c‎ | language
m (typo)
m (+generic for the list of uneval expressions)
Line 46: Line 46:
  
 
===Unevaluated expressions===
 
===Unevaluated expressions===
The operands of the two operators {{rlp|sizeof}}{{rev inl|since=c11| and {{rlp|_Alignof}}}} 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|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.

Revision as of 07:38, 9 January 2015

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), may designate objects or functions.

Contents

General

  • value categories (lvalue, non-lvalue, function designator) classify expressions by their values
  • order of evaluation of arguments and subexpressions specifies the order in which intermediate results are obtained

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)

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.

Other

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)

Any expression in parentheses is also classified as a primary expression: this guarantees that the parentheses have higher precedence than any operator.

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 char16_t, char32_t, (since C11)or wchar_t
  • floating constants are values of type float, double, or long double
  • string literals are sequences of characters of type char[], char16_t[], char32_t[], or wchar_t[] that represent null-terminated strings
  • compound literals are values of struct, union, or array type directly embedded in program code

Unevaluated expressions

The operands of the sizeof operator , the _Alignof operator, and the controlling expression of a generic selection,(since C11) 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.