(1) different compiler processing methods
Define macros are expanded during the preprocessing phase.
Const constants are used during the compile run phase.
(2) Different types and security checks
Define macros have no type, do not do any type checking, just expand.
Const constants have specific types that perform type checking during the compilation phase.
(3) different storage methods
The Define macro is only expanded, how many places are used, how many times it is expanded, and no memory is allocated.
Const constants are allocated in memory (either in the heap or in the stack).
(4) const can save space and avoid unnecessary memory allocation. For example:
1 #definePI 3.14159//Macro Constants2 ConstDoulbe pi=3.14159;//pi is not placed in ROM at this time ...3 DoubleI=pi;//allocate memory for PI at this time, no longer assigned! 4 DoubleI=pi;//macro substitution during compilation, allocating memory5 DoubleJ=pi;//No memory Allocations6 DoubleJ=pi;//Another macro replacement, another memory allocation!
(5) Improved efficiency. The compiler typically does not allocate storage space for ordinary const constants, but instead saves them in the symbol table, which makes it a constant during compilation, without the storage and read memory operations, making it highly efficient.
(6) Some integrated debugging tools can debug const constants, but cannot debug macro constants.
The difference between const and # define in C + + programming