Concept

How does the #define macro create an alias for a value?

ComputerScienceOne / Comments

"#define MILES_PER_KM 1.609\n\nThe macro defines an “alias” for the MILES_PER_KM identifier as the value 1.609. Essentially, the C preprocessor will go through the code and any instance of MILES_PER_KM will be replaced with 1.609. The advantage of using a macro like this is that we can use the identifier MILES_PER_KM throughout our program instead of magic numbers whose meaning and intent may not be immediately clear. Moreover, if we want to change the definition (say make it more precise, using 1.60934 instead) then we only need to change the macro instead of making the same change throughout our program. As a stylistic note: macro constants in C are usually associated with uppercase underscore casing as in our example. Also, the math standard library defines several macros for common mathematical constants such as π, e, and √2 (M_PI, M_E, and M_SQRT2 respectively) among others."

Related Ideas

How does the #define macro create an alias for a value? | ComputerScienceOne | Bifalgorithm | Bifalgorithm