First, #define宏定义
1. Macro definition, like this is the macro definition: #define PI 3.14159;
2. Macro Definition Format: Macro value #define Macro name is the macro to be defined, usually denoted by a meaningful name in uppercase, and the macro value is the constant represented by the macro. ;
3. #define是预编译指令;
4. The essence of the macro is an alternative.
5. When the precompiled program processes the source code, if it finds that the macro is used in the code, the macro value is used instead of the macro.
6. Benefits of using Macros: Use a macro to give the constant a birthright, in addition to avoiding multiple inputs repeated use;
7. Example:
Macros make your code more concise and straightforward
#define MIN 0
#define MAX 100
for (int i =min; i<max; ++i)
{//....
}
Situations where macros are not used
for (int i =0;i <100;++i)
{
//..
}
8. The problem with macros: because it is an unconditional substitution at preprocessing, and does not explicitly specify the data type of the constant, it is easy to bring convenience as well as problems. So the Const keyword is out of the field;
Second, the const keyword
1. The effect is this: const double pi= 3.14159;//defines a constant PI;
2. The format is: const data type constant name = constant value
3. Another use of the keyword, so that the value of the variable can not be modified, as long as the variable is defined by adding the const keyword;
4. Once a constant with const is defined, it can no longer be modified, or a compilation error will occur.
General above: The use of the Const keyword is recommended
#define宏, const keyword C + +