Explanation for #define:A #define is either an immediate value or a macro.
Explanation for constant:A constant is a value of any type which can never change.
You can delcare a pointer to a const, but not to a #define, although a #define could be a pointer
for eg: #define ADDRESS ((int *)0x0012)
So why we should use constant is as follows:
- they obey the language's scoping rules
- you can see them in the debugger
- you can take their address if you need to
- you can pass them by const-reference if you need to
- they don't create new "keywords" in your program.
In short, const identifiers act like they're part of the language because they are part of the language.
Within a module, a C compiler could optimize a const as if it were a #define, if there are no pointers declared to the constant.
In CPU terms, the const would become an "immediate" value.
Other alternatives is that a const variable could be
placed in the code area as opposed to the data area since it doesn't change.
On some machines, declaring a ponter to a constant could cause an exception if you tried to modify the constant via the pointer.
There are cases where #define is needed, but you should generally avoid it when you have the choice. You should evaluate whether to use const or #define based on business value: time, money, risk.