A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/algorithm/../numeric/../../c/language/constant_expression.html below:

Constant expressions - cppreference.com

Several varieties of expressions are known as constant expressions.

[edit] Preprocessor constant expression

The expression following #if or #elif must expand to

Character constants, when evaluated in #if-expressions, may be interpreted in the source character set, the execution character set, or some other implementation-defined character set.

Integer arithmetic in #if-expressions is performed using the semantics of intmax_t for signed types and uintmax_t for unsigned types.

(since C99) [edit] Integer constant expression

An integer constant expression is an expression that consists only of

(since C11) (since C23)

Integer constant expressions are evaluated at compile time. The following contexts require expressions that are known as integer constant expressions:

(since C99) (since C11) (since C23) [edit] Static initializer

Expressions that are used in the initializers of objects with static and thread_local storage duration or declared with the constexpr storage-class specifier(since C23) must be either string literals or expressions that may be one of the following

2)

a null pointer constant (e.g.,

NULL

).

3) address constant expression

, which is

  • by using the unary address-of operator
  • by casting an integer constant to a pointer
  • by array-to-pointer or function-to-pointer implicit conversion.

4) address constant expression of some complete object type, plus or minus an integer constant expression.

5)

a

named constant

which is, an identifier that is

or a postfix expression that applies the

.

member access operator to a named constant of structure or union type, even recursively.

6)

a

compound literal constant

, which is

A structure or union constant is a named constant or compound literal constant with structure or union type, respectively. If the member-access operator . accesses a member of a union constant, the accessed member shall be the same as the member that is initialized by the union constant’s initializer.

(since C23)

7) constant expression of one of the other forms accepted by the implementation.

Unlike with integer constant expressions, static initializer expressions are not required to be evaluated at compile time; the compiler is at liberty to turn such initializers into executable code which is invoked prior to program startup.

static int i = 2 || 1 / 0; // initializes i to value 1

The value of a floating-point static initializer is never less accurate than the value of the same expression executed at run time, but it may be better.

[edit] Floating-point constant expressions

Arithmetic constant expressions of floating-point types that are not used in static initializers are always evaluated as-if during run-time and are affected by the current rounding (if FENV_ACCESS is on) and report errors as specified in math_errhandling.

void f(void)
{
#pragma STDC FENV_ACCESS ON
    static float x = 0.0 / 0.0; // static initializer: does not raise an exception
    float w[] = { 0.0 / 0.0 };  // raises an exception
    float y = 0.0 / 0.0;        // raises an exception
    double z = 0.0 / 0.0;       // raises an exception
}
[edit] Notes

If an expression evaluates to a value that is not representable by its type, it cannot be used as a constant expression.

Implementations may accept other forms of constant expressions. However, these constant expressions are not considered as integer constant expressions, arithmetic constant expressions, or address constant expressions, and thus cannot be used in the contexts requiring these kinds of constant expressions. For example, int arr[(int)+1.0]; declares a VLA.

[edit] References
[edit] See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4