Constant Expressions

A constant expression is an expression with a value that is determined during compilation. That value can be evaluated at runtime, but cannot be changed. Constant expressions can be composed of integer, character, floating-point, and enumeration constants, as well as other constant expressions. Some constant expressions, such as string literals or address constants, are lvalues.

The C language requires constants in the following places:

In all of these contexts, except for an initializer of a file scope data definition, the constant expression can contain integer, character, and enumeration constants, casts to integral types, and sizeof expressions. Function-scope static and extern declarations can be initialized with the address of a previously-defined static or extern.

In a file scope data definition, the initializer must evaluate to a constant or to the address of a static storage (extern or static) object (plus or minus an integer constant) that is defined or declared earlier in the file. The constant expression in the initializer can contain integer, character, enumeration, or float constants, casts to any type, sizeof expressions, and unary address expressions.

The following show constants used in expressions:

Expression Containing Constant Constant
x = 42; 42
extern int cost = 1000; 1000
y = 3 * 29; 3 * 29

 



Types of Expressions