Macro represents a constant
If we want to write a procedure for all kinds of circulation, then π (3.14159) value will be used to be used. We clearly have no reason to change the value of π, so we should treat it as a constant, then we have to write 3.14159 long strings over and over again.
There must be a lazy way, and to promote this lazy, because write 3.14159 many times, it is inevitable.
This uses a macro. Macros can not only be used instead of constant values, but can also be used instead of expressions or even code segments. (Macro's function is very powerful, but it is also easy to make mistakes, so it is quite controversial.) Today we only talk about the function of replacing the constant value.
The grammar of the macro is:
#define macro name macro value
For example, instead of the π value mentioned earlier:
#define pai 3.14159
Note that macro definition is not a statement in a C or C strict sense, so the end of its row does not have to end.
The macro name is named, so we use PAI to represent π here, because C, C cannot use π characters directly. With the above statement, we can use PAI this macro to use PAI in the program where you want to use 3.14159.
As a suggestion and a common habit of programmers, the macro name often uses all uppercase letters.
Suppose there is a code:
Double ZC = 2 * 3.14159 * r; // Survey the circumference, where R is a variable representing the radius
Double MJ = 3.14159 * r * r; // Request round area
After defining the macro PAI, we can use this:
#define pai 3.14159
Double = 2 * PAI * R; // See the circumference, where R is a variable representing the radius
Double = pai * r * r; // seek a circular area
Use macro to replace constants, the benefits are:
1) Let the code are more concise
Of course, this depends on you for a proper name. In general, the name of the macro must pay more attention to clear intuitive significance, sometimes Never make it long.
2) Convenient code maintenance
Just as the 3.14159 mentioned above. You found that this π value is not enough, want to change to 3.1415926, then you only modify a macro, not all macros in the code.
Original macro:
#define pai 3.14159
Modified macro:
#define pai 3.1415926
The processing of macros is called "pretreatment" during the compilation process. That is to say, before formal compilation, the compiler must first replace the macro of the code, replace it with its corresponding macro value, this process is a bit you are a lookup replacement in the text processing software. After completing the pre-treatment, all the original "PAI" became an immediate number 3.1415926. So use the macro expression constant in the code, and the normal number is used in the end, and it is not clearly specified in the type of this amount. This is easy to bring some problems, so C uses another more stable way to replace the macro.
Constant definition
The format of a constant definition is:
Const data type constant name = constant value;
Compared to variable definitions, constant definitions must be started at const, in addition, constants must perform assignment while defining.
Const float pai = 3.1415926;
The role of Const is to indicate this amount (PAI) is constant, not variable.
The constant must specify a value at the beginning, then, in the later code, we do not allow the value of PAI, such as:
Const float pai = 3.14159; Double ZC = 2 * PAI * R;
PAI = 3.1415926; // Error! PAI can no longer be modified.
Double MJ = PAI * R * R;
If a constant is integer, you can omit the data type, such as:
Const k = 100;
Equivalent
Const Int K = 100;
Conversely, if the data type is not specified, the compiler is intended. such as:
Const k = 1.234;
Although you want K equal to a real shape, the value of the final K is actually 1. Because the compiler treats it as an integer.
We recommend that when defining variables, we clearly indicate type, regardless of it is integer or other type.
Const Int i = 100;
Const Double Di = 100.0;