Theory C language preprocessor - macros 2

Macro Definition 2
1, with the difference between the reference and the parameterized macro function
(1) is a pre-defined macro during processing, and the function during the compilation process. This distinction brings substantial difference is: macro definition is ultimately a call to the macro
place the macro body in situ expansion, and the function is a function of the jump to the calling function to perform, and then executing the jump back.
Note: The macro definitions and function of the maximum difference is: macro definitions are in place to expand, so there is no call overhead; and the function is executed to jump back again, so the function has a relatively large
call overhead. So macro definitions and function compared to the cost advantage is not called, there is no parameter passing overhead, so when the function body is very short (especially in only one sentence)
can be used to replace the macro definitions, such high efficiency.
(2) with parametric macros, and a significant difference with the reference function is: macro definition does not check the type of parameters, return values will not be included with the type; and a clear function parameter types
and return type. When we call the function the compiler will help us to do static type checking of parameters, if the compiler will find that we actually pass the time parameters and parameter declarations are not the same
error or warning report.
Note: The programmer does not match with less time to worry about the type of function used, because the compiler checks, if you do not match the compiler will prompt; use macros when programmers must pay great attention to
the actual mass participation and macro parameters consistent with the desired type otherwise, the compiler may not be an error, but the operation is wrong.
Summary: macros and functions strengths and weaknesses, advantages and disadvantages. In general, if the code more suitable for use and does not affect the efficiency of the function; but for those functions cost only one or two sentence
is too big, suitable for use with the macro parameters. But with the parameterized macro and disadvantages: does not check the parameter type.
2, and inline inline function key
(1) inline function achieved by adding keywords incline before the function definition.
(2) on the inline function is essentially function, so there are advantages function (inline function is that the compiler handles the compiler to help us to do static type checking parameters); but he
also has the advantage of parameterized macros (not call overhead, but in situ expansion). It can almost be considered: inline function is to bring the macro parameters static type checking.
(3) When we function within the function body is very short (for example, only one or two sentences) when we want to use the parameter type checking compiler to troubleshooting, but also hope that there is no
time to call overhead, is best used inline function.
3, macro definitions to implement conditional compilation (#ifdef #define #undef)
(1) program has DEBUG and RELEASE version version, the difference is whether the definition of the macro DEBUG compile time.

. 1 #include <stdio.h>
 2  
. 3  #define the DEBUG
 . 4  
. 5  // #undef the DEBUG     // unregister a macro if the macro is defined in front, the cancellation 
. 6  #ifdef the DEBUG
 . 7  #define Debug (X) the printf (X)
 . 8  #else 
. 9  #define Debug (X)
 10  #endif 
. 11  
12 is  int main ( void )
 13 is  {
 14      Debug ( " the this IS A Debug info \ n-. " );
 15      return  0 ;
 16 }

 

Guess you like

Origin www.cnblogs.com/jiangtongxue/p/11736751.html