For example, I saw source code like the following. Can we use #define
in a function? How does it work? (more information: this code is what I copied from
Sure. #define
is handled by the preprocessor which occurs well before the compiler has any sense of lines of code being inside functions, inside parameters lists, inside data structures, etc.
Since the preprocessor has no concept of C++ functions, it also means that there is no natural scope to macro definitions. So if you want to reuse a macro name, you have to #undef NAME
to avoid warnings.