1. What is pretreated
Compiling divided into four steps:
① .c file generation .i file, pretreatment;
② .s .i file generation file, the compiler;
③ .s .o file file generation, compilation;
④ .o file an executable file, link;
File: $ directly to the bottom of the file;
gcc -o main.i main.c -E
Generate a preprocessed file, -E representation only pretreatment;,
see the preprocessed file onlyvi main.i
The first thing to do pre-treatment: Expand the header file, the file can be displayed in .i, no longer appears in the form of include; then macro replacement.
2. Pretreatment of macros
#define R 10
The second step is the macro substitution pretreatment such int a = R;
a statement such as replacingint a = 10;
Do not think this is 10 int, 10 and think this is a string, the replacement process simple string to replace it;
Nature macro: string replacement occurs in pretreatment stage;
Example:
#include<stdio.h>
#define R 10
#define M int main()
M
{
int a=R;
printf("a=%d\n",a);
printf("hello world!\n");
return 0;
}
Programs can also run out.
In the pretreatment stage, does not consider the macro syntax compiler;
3 Pretreatment of macro function
#include<stdio.h>
#define R 20
#define M int main()
#define N(n) n*10
M
{
int a=R;
printf("a=%d\n",a);
int b=N(a);
printf("b=%d\n",b);
return 0;
}
operation result:
linux@ubuntu:~/workspace2/les1$ ./a.out
a=20
b=200
.i file int b=N(a);
has been replacedint b=a*10;
#define ADD(a,b) a+b
int main()
{
int a=20;
int b=200;
int d=ADD(a,b);
int e=ADD(a,b) * ADD(a,b);
}
The results of d may fulfill the functions of ADD function;
desired result e is 48400, the actual result is 4220;
Reason: .i file macro substitution result int e= a+b*a+b;
, computation time after the compilation will be executed, in the preprocessing stage It does not perform an arithmetic operation.
.C files need to be changed int e= (ADD(a,b))*(ADD(a,b));
(YY replication, p is pasted, u is withdrawn, ctrl + r revoked canceled);
? Macro function with the normal function of any difference
macro function does not return type and parameter types, the type can not be considered;
In the preprocessing stage, in addition to the macro, also it provides a conditional compilation functions may be different according to different conditions to compile part of the program, resulting in different code files, to the transplant procedure and for debugging.
4 . typedef
typedef is a keyword, you can give individual names from the variable type;
typedef int tni;
typedef int* p;
p q=NULL;// 写法等同于int *q = NULL;
Int * to type the name of the individual from p;
typedef and macro difference:
results of a semicolon 1.typedef;
2.typedef the pretreatment is not to be replaced, the file is not replaced .i;
below 3 can be used after the macro definition, have the effect typedef ? domain, the general body of the function in this
typedef usually give their own custom data types from the alias:
typedef unsigned long size_t;
Unused structure typedef:
struct stu{
};
struct stu a;
Structure using typedef:
typedef struct stu{
}stu_t;
stu_t a;