1. Simple macro definition
#define < macro name > < string >
Example: #define LEN 0.5
2. Macro definition with parameters
#define < macro name > (< parameter table >) < macro body >
Example: #define FUN (para) para*2
3. Example Analysis
1 #define VALUE2void main ()3{4 int n=value* VALUE; 5 printf ("%d", N); 6 }
What is the output of this code? 16? It might surprise you, as a result, 8. The reason is that the preprocessor simply replaces and expands the macro references that appear in the C source program file, and does not perform any computational processing. So the 4th line of code expands after n=2+2*2+2, the result is 8. To make the result 16, just define the macro as follows.
#define VALUE (+ +)
Similar problems can occur in macro definitions with parameters.
#define Fun (x) x*xvoid main () { int n=fun (2+2); printf ("%d", N);}
The output of this code is still 8. The reason is as above.
4. Three symbols: #,##,#@
#define Conn (x, y) x# #y#define tochar (x) #@x#define ToString #x
x# #y: represents x connection Y.
such as: int n=conn (12,89); The result of N is 1289;
char* str=conn ("abc", "Der"); Str results in str= "Abcder";
#@x: Represents a single quotation mark for x, and returns a const char.
#x: Represents a double quotation mark for X, and returns a string.
C Language Learning--macro definition