Define section:
Macros can be used not only to replace constant values, but also to replace expressions or evenCode. (Macros have powerful functions but are prone to errors, so their advantages and disadvantages are quite controversial .)
Macro Syntax:
# Define macro name macro value
Note that the macro definition is not a strict statement in the C or C ++ sense, so the end of the row does not end with a plus sign.
As a suggestion and a broad rangeProgramIn common, macro names often use uppercase letters.
Advantages of macros:
1) make the code more concise and clear
Of course, this depends on a proper name for the macro. Generally, the macro name should be more explicit and intuitive, and sometimes it is better to keep it longer.
2) Easy code Maintenance
Macro processing is called "preprocessing" during compilation ". That is to say, before official compilation, the compiler must replace the Macros in the Code with their corresponding macro values. In this process, you may find and replace them in the text processing software. Therefore, when macro expressions are used in the Code, the immediate number is used in the final analysis, and the type of this number is not explicitly specified. This may cause some problems, so c ++ uses another more secure method to replace the macro function.
Const Section
Constant Definition Format:
Const data type constant name = constant value;
The constants defined by const have data types. constants defining data types are convenient for the compiler to perform data checks, so that programs may encounter Errors for troubleshooting. A constant must be specified at the beginning. Then, in future code, we cannot change the value of this constant.
Differences between the two:
Memory space allocation. When define is used for macro definition, no memory space is allocated. during compilation, It will be replaced in the main function, but it will be replaced simply without any check, such as the type and statement structure, that is, macro-defined constants are purely placement relationships, for example, # define null 0; when the compiler encounters null, it always uses 0 instead of null. It has no data type. (For more information, see C language books for preprocessing or msdn. the constants defined by const have data types. constants defining data types are convenient for the compiler to perform data checks, so that programs may encounter Errors for troubleshooting, therefore, the difference between const and define is that const defines constants to eliminate the security of programs.
Http://www.phpweblog.net/maple094/archive/2008/05/30/4054.html