You can define your own names for constants that you use very often without having to resort to memory-consuming variables, simply by using the #define preprocessor directive. Its format is: ...
Declared constants (const)
Here, pathwidth and tabulator are two typed constants. They are treated just like regular variables except that their values cannot be modified after their definition.
...
What concerns me is the suggestion that #defines are better because they take up less space. From what I understand, the optimizer is not forced to put a value in the read-only data segment and use it from there if it doesn't want to.
Am I reading the article wrong?
Andy
PS This exciting program
1 2 3 4 5 6 7 8 9 10
constint a = 100;
int test1(int m) {
return (a * m);
}
int main() {
int m = 2;
return test1(m);
}
I agree, it would be good if the article explicitly suggested using const over #define (it could say that type safety is more important than the minimal difference in memory usage and speed, especially when often the compiler will optimise const values away). Even more important is the lack of a section for enumerated constants. IMO enum should be preferred over both const and #define for numeral constants.