...
In this compliant solution, pi is declared as a const-qualified object, allowing the constant to have scope.
| Code Block | ||
|---|---|---|
| ||
const float const pi = 3.14159; float degrees; float radians; /* ... */ radians = degrees * pi / 180; |
...
In this non-compliant code example, max is declared as a const-qualified object. While declaring non-integer constants as const-qualified object is the best that can be done in C, for integer constants we can do better. Declaring immutable integer values as const-qualified objects still allows the programmer to take the address of the object. Also, const-qualified integers cannot be used in locations where an integer constant is required, such as the value of a case constant.
| Code Block | ||
|---|---|---|
| ||
const int const max = 15; int a[max]; /* invalid declaration outside of a function */ const int const *p; p = &max; /* legal to take the address of a const-qualified object */ |
...
| Code Block | ||
|---|---|---|
| ||
enum { max = 15 };
int a[max]; /* OK */
const int const *p;
p = &max; /* error: '&' on constant */
|
...