(1) Different define macros are processed by compilers in the preprocessing phase. Const constants are used in the compilation and running phase. (2) Different Types and security checks define macros have no types, and do not perform any type checks, just expand. Const constants have specific types. during compilation, the type check is performed. (3) Different Storage Methods define macros only expand. The memory will be expanded as many times as there are places to use and will not be allocated. Const constants are allocated in memory (either in heap or stack ). (4) const can save space and avoid unnecessary memory allocation. For example: # define PI 3.14159 // constant macro const doulbe Pi = 3.14159; // pi is not put into ROM at this time ...... double I = PI; // at this time, the PI is allocated with memory and will not be allocated later! Double I = PI; // macro replacement during compilation, allocating Memory double J = PI; // no memory allocation double J = PI; // then macro replacement, allocate memory again! Const defines constants. From the assembly point of view, only the corresponding memory address is given, rather than the number of immediate values as # define. Therefore, the const-defined constants have only one copy during the program running, while the # define-defined constants have several copies in the memory.
Define and const