How to Use macro definition in C Language
Explanation of macro replacement in C Language
First, let's look at the following question:
# Include
# Define PRINT_CLINE () printf ("% d ",______)
Int main (void)
{
PRINT_CLINE ();
PRINT_CLINE ();
Return 0;
}
Fill in the appropriate code at the horizontal line so that the output of the above Code is 34.
I think most people do not have a clear idea to answer this question when they see it. When I saw this question, I came up with a variety of methods to solve it, and finally failed the compiler check. For example, one of the methods I thought about was:
(Int static I = 2, I ++)
I want to define a static variable at this position, assign the initial value to 2, and add an I ++ variable. According to the calculation rules of the comma operator, the value of the entire expression should be the value of the last one. The idea is indeed perfect, but it cannot pass the compiler! It is illegal to use a statement to define variables at this position! After thinking for a long time, I cannot find a solution. So I reorganized the macro definition usage and finally found the answer. The following describes how to use the macro definition step by step:
1. Simplest replacement
# Defind Pi 3.14159
The preceding statement is a commonly used simple replacement. In the pre-processing stage, the Pi in the program code is replaced with the following 3.14159.
2. Macro replacement with Parameters
# Define MAX (a, B) (a)> (B )? (A) :( B)
The macro with parameters is replaced. When the MAX macro is used in the Code, the parameters in the brackets will follow the macro replacement. Of course, you must note that you must add () to each parameter, otherwise it may produce a marginal effect.
3. Character replacement character :#@
Let's take a look at the following code:
# Include
# Define PT_CHAR (x) printf ("% c", # @ x );
Int main (void)
{
PT_CHAR (d );
Return 0;
}
Program output is d. Maybe you will ask, can you simply replace # @ x with x in the macro definition? A rough look at this statement is indeed acceptable, however, when the macro is replaced, the PT_CHAR (d) compiler considers d as a passed parameter. Since d is not a constant, the compiler has to consider it as a variable, but then the problem arises, and d has not been defined!
4. String macro replacement character :#
This is a bit similar to the above, such as the following two lines of code:
# Define PT_STRING (x) printf ("% s", # x)
PT_STRING (Hello !);
If the output is Hello! # X is required. If not, the compiler will set Hello according to the previous analysis! It can be understood as a variable, but even so, this! It cannot be part of a variable, so the compiler reports many errors.
5. Connection macro replacement :##
# Define p (n) printf ("symbol" # n "= % d", symbol # n)
Int symboa9 = 9;
P (9 );
The above output is symboa9 = 9, and symbol # n connects symbol and n (that is, the passed 9) to form the variable symbo9.
6. Several predefined Macros in the ANSI standard
_ LINE __: Insert the current source code LINE number into the source code;
_ FILE __: insert the name of the current source FILE into the source FILE;
_ DATE __: Insert the current compilation DATE into the source file
_ TIME __: Insert the current compilation TIME into the source file;
_ STDC __: when the program strictly follows the ansi c standard, the ID is assigned 1;
_ Cplusplus: This identifier is defined when a C ++ program is compiled.
Here, the answer to the above question comes out. The _ LINE _ in the standard definition macro represents the LINE number in the source code modification code, you can use this macro to conveniently implement the above functions.
Answer: # define PRINT_CLINE () printf ("% d", _ LINE__-4)