Theory C language preprocessor - macros 1

A macro definition
rule definition and use of macros parsing
(1) parsing rules macro definition is: replaced by a preprocessor preprocessing stage, this alternative is replaced intact.
(2) Alternatively macro definitions recursively until the replacement value itself is no longer out of a macro so far.
M 10 #define
#define the NM
(3) defines a proper macro formula itself divided into three parts, the first part is #define, the second part is a macro name, all the rest of the third portion.
(4) macro parameters, called parameterized macro. Parameterized using macros and parameterized function very much like, but there are some differences in use. When you define a macro with parameters, each parameter
in the macro body must be bracketed when referring to the last overall plus brackets, parentheses are indispensable.
X-#define (A, B) ((A) * (B))
2, macro definitions Example 1: MAX macro, seeking a larger number 2
#define MAX (a, b) ( (a)> ( ? b) (a): ( b))
key:
 the first point: to conceivable to use to complete the ternary operator.
 The second point: Note the use of brackets
3, macros example 2: SEC_PER_YEAR, macro definitions denoted by the number of seconds in a year
#define SEC_PER_YEAR (31536000) // feasible, but not recommended
#define SEC_PER_YEAR (365 * 24 * 60 * 60) // error, the default type int, over a range of storage types, int itself is a symbol of
#define SEC_PER_YEAR (365 * 24 * 60 * 60UL) // correct wording, would have become a symbol unsigned, expand the scope of a time

Guess you like

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