In various C code, I see constants defined like this:
#define T 100
Whereas in C++ examples, it is almost always:
const int T =
There is no requirement that T
be stored "in memory" in the second case, unless you do something like take the address of it. This is true of all variables.
The reason the second one is better is that the first will frequently "disappear" in the pre-processor phase so that the compiler phase never sees it (and hence doesn't give it to you in debug information). But that behaviour is not mandated by the standard, rather it's common practice.
There's little need to use #define
statements any more other than for conditional compilation. Single constants can be done with const
, multiple related constants can be done with enum
and macros can be replaced with inline
functions.