const define the difference between the C language and

const define the 
symbol variables define pre-compiled instructions, character replacement precompiled only when, after the pre-compiler constants symbol does not exist, for example, define 3.1415926 PI, PI does not exist after compiling all of the transducers are PI It became 3.1415926. PI and no storage unit. Const variable and often take up variable storage unit, there is a variable value, but the value can not be changed, it has the advantages of variable symbols, and more convenient. In fact, there is a static, but this is the C language, it is not a.
Specifically divided into:
(. 1) defined by the constant const tape type, tape type does not define,
(2) const is compiled, run time function , and define a function in the preprocessing stage of compilation
(3) define simply the replacement string, no type checking. Const and a corresponding data type is to be determined, the number of low-level errors can be avoided. 
define just a simple string replacement will lead to boundary effects, such as the definition
                #define A 1
                #define A + B 3
                #define CA / B * 3
then c is how much? c = A / B * 3 = A / A + 3 * 3 = 1/1 + 3 * 3 = 10;
so that when used are used directly enclosed in parentheses, are not afraid of the boundary effect.
(4) define the pre-code space occupied, the data segment and occupies space const. 
(5) const constants can be debugged, it defines are not debug, mainly pre-compilation stage has been replaced, and there is no time to debug it.
(6) const can not be redefined, we can not define two different, but define it perkier, and it canceled the definition of a symbol through undef, then redefined.
(7) define header can be used to prevent duplicate files referenced const not. For example:
#ifndef GRAPHICS_H // prevent graphics.h repeated references 
#define GRAPHICS_H
...... Code ...... 
#endif
to say about the repeated references here are some header files compiled only increased the workload of the work, it will not cause much of a problem, only the low efficiency of some of the compiler, but for large projects compile low efficiency it would be a very painful thing; some header files contain repeat, will cause an error, such as global variables defined in the header file that causes repeated defined
, although the C specification allows, but does not recommend such use ah.
(8) use define the code that will look very simple, and can not realize the function const 
e.g., MFC achieving six core mechanism, extensive use define 
. 1, MFC initialization procedure 
2, the runtime type identification (RTTI) 
. 3 dynamically create 
4, permanent preservation 
5, message map 
6, messaging
define the difference between the direct and const at some point there is a certain similarity with the C language and JAVA language: 
(1) JAVA internal height of the object in the package, such as mechanisms for recovery of the memory is done automatically, the programmer does not need more ingenuity; C language, each application when we use the pointer is a space, we need to consider carefully this will have to use the pointer later, now is not it can be released, will not become a field guide and so on, all of the memory-related issues need to be considered programmer, but the benefits are, C language can be direct and convenient operating memory, which is a double-edged sword, with well more with less, used poorly, memory leaks, pointer runaway problem everywhere.
(2) differences in point C language and JAVA language analogy to define and const, it is emphasized that define the role of very strong, although it is not the type of testing, not debugging, but also consider boundary effects, but because there is no type detection, pre compilation is complete, which makes its use more flexible and more powerful. Saying watching others learn how to pick the type, reprint it seems not, nor is originality, but not translated, it can not be others?

Guess you like

Origin blog.csdn.net/mad_sword/article/details/79809263