Macro substitution in C language
First look at a question:
#include <stdio.h>
#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 that when people see this problem, there is no definite idea in the mind to answer it. When I saw the problem, I came up with a variety of ways to answer it, and eventually I didn't pass the compiler's check, for example, one of the ways I thought about it was:
(int static i=2, i++)
Thinking about defining a static variable in this position, and assigning an initial value of 2, and finally adding a i++, according to the operation rules of the comma operator, the value of the entire expression should be the last item, the idea is really perfect, but this is not the compiler! It is illegal to use a statement that defines a variable in this position! Thought for a long time really can not come out of the way. So we re-organized the use of macro definitions, and finally found the answer. Here's a step-by-step procedure for using a macro definition:
1. The simplest replacement
#defind Pi 3.14159
The above statement is a relatively common simple replacement, in the pre-processing phase, where the PI appears in the program code is replaced by the following 3.14159.
2. Macro substitution with parameters
#define MAX (A, B) (a) > (b)? (a):(B)
This is the replacement of the macro with the parameter, and when the Max macro is used in the code, the parameters inside the parentheses follow the macro substitution. Of course, the point of this substitution is to remember to add () to each parameter, otherwise it may have marginal effects.
3. Character substitution: #@
Let's look at the following code:
#include <stdio.h>
#define PT_CHAR (x) printf ("%c", #@x);
int main (void)
{
Pt_char (d);
return 0;
}
The output of the program is D. Perhaps you will ask, that is directly in the macro definition of #@x with X to replace it is not possible, a rough look at this is true, but when the macro replacement Pt_char (d) The compiler will think D is a passed parameter, since D is not a constant, then the compiler will have to think it is a variable, But then the problem comes out and D doesn't define it!
4. String macro substitution: #
This is a bit similar to the above, such as the following two lines of code snippet:
#define PT_STRING (x) printf ("%s", #x)
Pt_string (hello!);
If you want to achieve the output for the purpose of hello!, it is necessary to use #x, if not used, according to the previous analysis, the compiler will understand the overall hello! as a variable, but even if this understanding, this is not a part of the variable, ah, so the compiler will report more errors.
5. Connection macro substitution: # #
#define P (N) printf ("symbol" #n "=%d", symbol# #n)
int symbol9 = 9;
P (9);
The above output is a symbol9=9,symbol# #n将symbol和n (that is, passed over 9), which makes up the variable SYMBOL9.
Several standard pre-defined macros in the 6.ANSI standard
__LINE__: Inserts the current source code line number in the source code;
__FILE__: Inserts the current source filename in the source file;
__DATE__: Insert the current compilation date in the source file
__TIME__: Inserts the current compile time in the source file;
__STDC__: The identifier is assigned a value of 1 when the program strictly complies with the ANSI C standard;
__cplusplus: This identifier is defined when you write a C + + program.
See here, the answer to the above question is also out, the standard predefined macro __line__ represents the source code in the code to change the line number, the use of this macro can be very convenient to implement the above functions.
Answer: #define Print_cline () printf ("%d", __line__-4)
How to use macro definitions in C language