Skip to content

Latest commit

 

History

History
51 lines (39 loc) · 2.27 KB

c-constant-expressions.md

File metadata and controls

51 lines (39 loc) · 2.27 KB
title ms.custom ms.date ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager ms.workload
C Constant Expressions | Microsoft Docs
11/04/2016
cpp-language
article
C++
constant expressions, syntax
constant expressions
expressions [C++], constant
d48a6c47-e44c-4be2-9c8b-7944c7ef8de7
10
mikeblome
mblome
ghogen
cplusplus

C Constant Expressions

A constant expression is evaluated at compile time, not run time, and can be used in any place that a constant can be used. The constant expression must evaluate to a constant that is in the range of representable values for that type. The operands of a constant expression can be integer constants, character constants, floating-point constants, enumeration constants, type casts, sizeof expressions, and other constant expressions.

Syntax

constant-expression:
conditional-expression

conditional-expression:
logical-OR-expression

logical-OR-expression ? expression : conditional-expression

expression:
assignment-expression

expression , assignment-expression

assignment-expression:
conditional-expression

unary-expression assignment-operator assignment-expression

assignment-operator: one of
= *= /= %= += -= <<= >>= &= ^= |=

The nonterminals for struct declarator, enumerator, direct declarator, direct-abstract declarator, and labeled statement contain the constant-expression nonterminal.

An integral constant expression must be used to specify the size of a bit-field member of a structure, the value of an enumeration constant, the size of an array, or the value of a case constant.

Constant expressions used in preprocessor directives are subject to additional restrictions. Consequently, they are known as "restricted constant expressions." A restricted constant expression cannot contain sizeof expressions, enumeration constants, type casts to any type, or floating-type constants. It can, however, contain the special constant expression defined (identifier).

See Also

Operands and Expressions