Program translation environment
Execution environment of the program
Explain in detail C Compilation of language program + link
predefined symbol
preprocessor directives #define
Comparison of macros and functions
Preprocessor Operators # and ## Introduction of
Command definition
preprocessor directives #include
Program translation environment
Translation environment and execution environment of the program
At any c There are two different environments in the implementation of
1. The translation environment is where the source code is converted into executable machine instructions
2. The execution environment is the actual execution of code
Execution environment of the program
3. Operating environment
1). The program must be loaded into memory , In an operating system environment , This is usually done by the operating system ,
In an independent environment , The loading of the program must be arranged manually , It may also be done by putting executable code into read-only memory ( Embedded )
2). The execution of the program begins , Then call main function
3). Start executing program code , At this time, the program will use a runtime stack ( Stack frame )( Does not include heaps ), Recycle his address after using it , Store local variables and return addresses of functions ,
Programs can also use static (static) Memory , Variables stored in static memory keep their values throughout the execution of the program .
4). Termination procedure , Normal termination main function , May also terminate unexpectedly
Explain in detail C Compilation of language program + link
predefined symbol
__FILE__ Print the path where the current file is located
__LINE__ Print the function where the current code is located
__DATE__ Print current date
__TIME__ Print current time
preprocessor directives #define
define Defining symbols , It can improve the readability of the code
Not only can you define numbers , You can also define codes , keyword , Or symbols ,
#define m 100 #define ret register// use ret To replace registr #define do_forever for( ;
;)// Dead cycle #define x int* #include<stdio.h> int main() { ret int b = 10;
do_forever; int a = m;//100 x p=&a;//p by int* Pointer variable of return 0; } #define x
int * typedef int * INT #include<stdio.h> int main() { x a,b; INT c,d;
// Only b Not a pointer // because define Only complete code replacement , Replaced with int* a,b//a Is a pointer ,b Not a pointer // and typedef Is a rename of a type ,
return 0; }
Comparison of macros and functions
define Definitional macro , The same is to complete the replacement
#define The mechanism includes a provision , Replace constants with text , This form of implementation is called macro , Or define macros ,
The left parenthesis of the parameter must be immediately adjacent to the function name , If there is a space in the middle , Will be interpreted as stuff Members in , Don't be stingy with parentheses
#include<stdio.h> // Don't be stingy with brackets #define square(x) x*x// It is suggested that x Expo site (x), Treat it as a whole , Parentheses are important
#define double(x) (x)+(x)// Take it as a whole #define doubl(x) ((x)+(x)) int main() {
printf("%d\n", square(3));// Will be replaced by printf("%d",3*3); printf("%d\n", square(3 +
1));//7!=16, Will be replaced , Macro parameters are replaced , Not calculated , Send it directly without any calculation // Will be replaced by printf("%d",3+1*3+1))//7
// With parentheses, it becomes (3+1)*(3+1) printf("%d", 10 *
double(4));// There is also a problem with this writing , Will be replaced by 10*(4)+(4)=44 printf("%d", 10 * doubl(4));// That's it ;
return 0; }
When calling macros again , First, check the parameters , Yes no define Defined symbols
Macros cannot be recursive , And functions can
When pretreatment , String constants cannot be replaced , as printf(“”) Not replaced in
#define m 100 #include<stdio.h> int main() { int c = max(101, m);// Macro replacement ,
printf("m=%d", m);// In parentheses m Not replaced return 0; }
Preprocessor Operators # and ## Introduction of
# You can insert parameters into strings
#define print(x) printf("the value of " #x " is
%d\n",x)//#x Will become x String for the content of "a" #include<stdio.h> int main() { printf("hello
world\n"); printf("hello""world\n");// The result is the same , // write 3 individual printf() A little redundant , int a = 10;
print(a); //the value of a is 10 int b = 20; print(b); //the value of b is 20
int c = 30; print(c); //the value of b is 30 printf(""); return 0; }
## Two symbols can be combined into one symbol , The two symbols are connected together
#define cd(x,y) x##y #include<stdio.h> int main() { int c = 100; printf("%d",
cd(c, 120));// replace with 100120 return 0; }
Macro parameters with side effects
First of all, what are side effects
int a=1;
int b=a+1;//b=2,a=1, No side effects
int b=++a//b=2, and a=2 there a There are side effects ,
#include<stdio.h> #define max(x,y) (x)>(y)?(x):(y) int main() { int a = 5; int
b = 8; int c = max(a++, b++); //int c = (a++) > (b++) ? (a++) : (b++); replace
//5>8 Not established , Execute judgment statement ,a and b Just add 1,a=6,b=9, Then execute b++, but b++ Is first used in ++ //c=9, After execution b=10 printf("%d",
c);//9 return 0; }
Comparison of macros and functions
1. Macros are better than functions in terms of program size and speed
2. more importantly , Function has data type restrictions on definition , If defined int Type data , be double Type data cannot be transferred to parameters , Macros are type independent ,
3. Naming rules , All macro names are capitalized , Function names are not all capitalized ( Unwritten rules )
Command definition
#undef Remove a macro definition
#if Add constant expression , by 0 If it is true, go down
#endif by #if End flag of
#ifdef If defined, execute the following code
#ifndef If it is not defined, execute the following code
#undef// Remove a macro definition #define m 100 #include<stdio.h> int main() { int a = m; #undef
m// Define m Cancel printf("%d", a);//a There is no value return 0; } //#if
Add constant expression ( wrong 0 Execute it before it is true , until #endif stop it ) //#endif by #if End flag of //#if 0// It is equivalent to commenting out the following code , #define
print 1 int main() { #if 1-2//1 Execute if true ,0 It's false and not implemented , wrong 0 True #endif #if
print//print by 1 If true, execute the following code printf("hehe "); #endif return 0; }
#include<stdio.h> int main() { #if
1==1// Judgment established , Just go ahead , Select only one from multiple branches , Skip to after selecting #endif go printf("hh"); #elif 1 == 2
printf("haha"); #else printf("hehe"); #endif return 0; } // Determine whether it is defined
#include<stdio.h> int main() { #ifdef test// If test Defined , Now participate in the implementation , Give a 0 it's fine too
printf("test"); #endif #ifndef hh// If hh Undefined , Let's participate in the compilation printf("hehe"); #endif return
0; }
preprocessor directives #include
File contains
1.<> Go directly to the directory where the header file of the library function is located
2."" Formal include file ,(1). Find the directory under the code you wrote ,(2). If 1 can't find , Find it in the header file directory of the library function
Nested file contains
A header file is included twice , A little wordy
#pragma once// Header file contains only once , No matter how many you write , Add this sentence only once
In header file ifndef,define ,endif It is used to prevent files from being included multiple times
Technology
Daily Recommendation