...
Code Block |
---|
|
const float pi = 3.14159f;
float degrees;
float radians;
/* ... */
radians = degrees * pi / 180;
|
Noncompliant Code Example
Wiki Markup |
---|
In this noncompliant code example a macro is used to define a maximum value, but there is no type information associated with the macro \[[Dewhurst 02|AA. C References#Dewhurst 02]\]. |
Code Block |
---|
|
#define MAX (1<<16)
// ...
void f(int);
void f(long);
// ...
f(MAX); // which f?
|
The value 1 << 16
could be an int
or a long
depending on the platform. As a result, this code becomes platform dependent.
Compliant Solution
Using a constant associates type information with the value and eliminates the possibility of confusion.
Code Block |
---|
|
int const max = 1<<16;
// ...
void f(int);
void f(long);
// ...
f(max);
|
Exceptions
DCL00-EX1: It is acceptable to define valueless macros to serve as 'inclusion guards'. That is, the macro serves to control the multiple inclusion of header files, as in the following example:
...