Any one ANSI C, There are two environments , A translation environment and a running environment .

        The operating environment is equivalent to windows, Translation environment is equivalent to compiler .

one , Program compilation and preprocessing :

A program from code to execution , To carry out a series of complex work , stay vs2022 In the compiler of , We have finished all these works . Only the implementation steps are briefly discussed here ( If you want to know more about recommendations 《 Self cultivation of programmers 》).     

vs2022 Is an integrated development environment (IDE), Its internal integration and compiler (cl.exe) And connectors (link.exe), To help users compile code quickly and conveniently , Finally, the executable program is generated .exe. Here is the step of generating executable program , It can be collectively referred to as compilation .

        However, on some compilers, you need to compile and process them manually .

Compilation environment :

        Here is a code file test.c Generated an executable test.exe  

  step :

1. Pretreatment ( precompile )  Generate Middleware  test.i

Here you will find , Because the middleware loads the header file , There are more than 60000 lines of code . Also deleted comments . And put #define The defined symbol is replaced .

2. The compilation results are saved in test.s in . middleware test.s What is saved in is the assembly code of the source code .

3. Compilation complete , Results saved in test.obj in . It is a pile of binary instructions and forms a symbol table .

4. Finally, link , At this point, the compiler can judge whether the external function is called correctly . Comparison symbol table , If the corresponding function is not found in the symbol table, an error will be reported . 

Operating environment :  

1. A program to be executed , Must load memory . stay windows lower , The system helped us load it , You need to burn manually on other platforms .

2. Start of procedure main() function .

3. Start executing program code .

4. Termination procedure .

two , Pretreatment

        #include and #define Are preprocessing instructions , One is used to load header files , One is for macro definition .

        stay vs There are many macro definitions inside the compiler

* __FILE__            Current file
* __LINE__           Current code line
* __TIME__          Time when the file was compiled
* __DATE__          Date the file was compiled
* __func__            Current function name
* __STDC__         Compiler compliance ANSI C, Its value is 1

        Of course, we can also define a macro ourselves , At this time, you need to use the preprocessing command #define.

          Here is a definition MAX(x,y)
, You will find that compared with writing a function of comparative size , This kind of writing is simple and convenient . But be careful ,#define It does not receive parameters like a function , It's a simple symbol replacement .

        The second file is when the program is compiled , Generated Middleware . Here you will find here #define Defined macro , Completely replaceable function .
  It doesn't calculate inside like a function , So there is no consumption in the stack area , So the speed is faster than the function .

        But because it is a simple symbol replacement , Therefore, we must pay attention to the priority of symbols here


        For example, I just want to calculate here 10 Multiply by the number after comparison , But after the replacement here, I found that 10*10 It was calculated first , Will cause errors . So here we should pay attention to the need to add parentheses . 

          But if only a single parameter is included , There will still be errors , So we need to enclose the whole here

        and ,#define Defined macro , The input parameters cannot have side effects (a+1 No side effects , however a++ Have side effects , It has changed a Value of itself )
#define MAX(x,y) ((x)>(y)?(x):(y)) int a=5; int b=8; int c=MAX(a++,b++);

        You'll actually find it here , After the previous variable calculation , Affecting the following values . The value obtained is different from the original value .


        Corresponding to a function, its parameter data has a fixed type , To make different types of comparisons , To define multiple functions , But the macro has no type check , Can accept any type of data .

        Macros cannot be debugged like functions , Macros cannot be recursive .

        So when you want to define a macro , You should choose whether to use it or not according to your own needs . 

#undef: Remove macro definition
#define NUM 10; #undef NUM
three , Conditional compilation :
#if Constant expression //... ​#endif #if Constant expression //... #elif Constant expression //... #else Constant expression //...
#endif #define NUM 1 #if NUM==1 printf("1\n"); #elif NUM==2 printf("2\n");
#else printf("3\n"); #endif
        The meaning of this code is , If NUM be equal to 1, Then print 1, If NUM be equal to 2, Print 2, Otherwise print 3. You will find that if() Statements are very similar .
#if defined(MAX) //... #endif // Equivalent to the following #ifdef MAX //... #endif
        The meaning of this code is , If MAX Defined by macro , Then execute the following code .
#if !defined(MAX) //... #endif // Equivalent to the following #ifndef MAX //... #endif
        The meaning of this code is , If MAX Not defined by macro , Then execute the following code .
#if defined(OS_UNIX) #ifdef OPTION1 unix_version_option1(); #endif #ifdef
OPTION2 unix_version_option2(); #endif #elif defined(OS_MSDOS) #ifdef OPTION2
msdos_version_option2(); #endif #endif
        Conditional compilation instructions can be nested , But you will find that the readability of the code is very poor , It's hard to understand .

PS: get into vs The built-in library function will find , Conditional compilation in vs The bottom layer is very common .

Avoid repeated references to header files :
1.#pragma once
#pragma It is also a preprocessing instruction , Add one here once, It is usually written at the top of the header file , It means that the header file is loaded only once . However, this writing method may not be supported on other compilers . So there is a common way to write it .
#ifndef _TEST_H #define _TEST_H #includ<stdio.h> //... #endif
          This writing method can also prevent multiple loading of header files , And good compatibility .

©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?