Home > Back-end >  The following a Duan Hong definition and explanation
The following a Duan Hong definition and explanation

Time:11-19

Typedef struct cmd_tbl_s cmd_tbl_t;

# define Struct_Section __attribute__ ((unused, section (". Cli_cmd ")))

# define REGISTER_CMD (name, maxargs, handler, the usage) \
Const cmd_tbl_t cli_cmd_ # # name Struct_Section={# name, maxargs, handler, the usage}

CodePudding user response:

Thank invited,

This code is not complicated, macro definition you also not strange, the emphasis is on __attribute__, # and # #

# and # # is not difficult, this is the C preprocessor token, you should also know,

As a result, your problem is mainly __attribute__, this mark is not C language symbols, but extended by GCC mark, doubling the GCC manual: https://gcc.gnu.org/onlinedocs/10.2.0/or Internet search, there is no longer Russell,

CodePudding user response:

# and # # in the macro definition - the macro
#include
# # # define f (a, b) b
# # define g (a) a
# define h g (a) (a)
Int main ()
{
Printf (" % s \ n ", h (f) (1, 2));//12
Printf (" % s \ n ", g (f) (1, 2));//f (1, 2)
return 0;
}
When the macro expansion:
If a macro definition begins with #, no expansion parameters, direct replacement,
Therefore, g (f) (1, 2) - & gt; # f (1, 2) - & gt;" F (1, 2) ";
If you don't begin with # macro definition, a parameter, directly replace, from outer to inner, if encounter is # macros in the beginning, not the layer, move on to the outer,
From the outer to the inner, if encounter is not # macros in the beginning, the inner continue to go, until the most layer, has been carried out to the outer layers,
Therefore, h (f) (1, 2) - & gt; H (12) - & gt; G (12) - & gt; # 12 - & gt;" 12 ",
PS:
# # in the macro definition, characters connection
Such as # # # b# c is equivalent to "ABC"
# at the beginning of the macro, macro expansion is said differently
# a is equal to "a"
# ABC is equivalent to "ABC"
Complex:
#include
# # # define f (a, b) b
# # define g (a) a
# define h g (a) (a)
Int main ()
{
Char a='a';
CoutCoutPrintf (" % s \ n ", h (f) (1, 2));//12
Printf (" % s \ n ", g (f) (1, 2));//f (1, 2)
Printf (" % s \ n ", g (h (f) (1, 2)));//h (f) (1, 2)
Printf (" % s \ n ", h (g (f (1, 2))));//"f (1, 2)"
Printf (" % s \ n ", h (h (f) (1, 2))); 12 "//"
system("pause");
return 0;
}
After pretreatment: (added in the compiler options/EP/P compiler generated. After I file, GCC plus - E)
Int main ()
{
Char a='a';
Cout<& lt;" A "& lt; Cout<& lt;" G (a) "& lt; Printf (" % s \ n ", "12");
Printf (" % s \ n ", "f (1, 2)");
Printf (" % s \ n ", "h (f) (1, 2)");
Printf (" % s \ n ", "" f (1, 2) \ "");
Printf (" % s \ n ", "" \ "");
system("pause");
return 0;
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Macro analysis
1. The # # operator
The # # operator it is used for representing the parameters before and after its connection as a preprocessing tokens, for it doesn't appear that has been on behalf of the beginning and end,
Example:
S # # # define concat (s, t) t
# define AAA ABC
Concat (A, AA)
Will be replaced with
ABC
Rescanning and replace
2.All the parameters in the replacement list after replacement, the preprocessor token sequence to scan the results for macro to replace of them,
When is replace the macro in the replacement list found itself, no longer to replace, in any replacement is nested macro encountered in the process of replacement is replaced macros to replace its no longer (to prevent recursive),
Example:
# define ROOT AAA CCC
# define AAA ROOT
The ROOT
Will be replaced with
ROOT CCC
  • Related