Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of compiling and preprocessing of C language Program

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "C language program compilation and preprocessing example analysis". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Translation environment and execution environment of the program

In any implementation of ANSIC, there are two different environments: translation environment and execution environment

Translation environment: the source code is converted into executable machine instructions.

Execution environment: the code is actually executed.

1. Translation environment

Each source file that makes up a program is converted to an object file (object code) by compilation.

Each object file is bundled together by a linker to form a single and complete executable program.

The linker also introduces any function used by the program in the standard C function library, and it can search the programmer's personal library and link all the required functions to the program.

two。 Operation environment

The execution environment of the program:

1. The program must be loaded into memory. In an environment with an operating system: this is usually done by the operating system. In a separate environment, the loading of the program must be arranged manually, or it may be done by placing the executable code into read-only memory.

two。 The execution of the program begins. The main function is then called.

3. Start executing the program code. At this point the program will use a runtime stack (stack) to store the function's local variables and return address. Programs can also use static memory, where variables retain their values throughout the program's execution.

4. Terminate the program. The main function is terminated normally or unexpectedly.

Preprocessing and detailed interpretation of predefined symbols

# define

# define definition Identifier

Syntax:

# define name stuff

# define MAX 100#define REG register / / create a short name for register # define do_forever for ( ) / / replace an implementation # define PRINT printf ("file:%s\ tline:%d\ tdate:%s\ time:%s\ n",\ _ _ FILE__,__LINE__,__DATE__,__TIME__) / / if the defined stuff is too long, it can be written in several lines, followed by a backslash (continuation) except the last line.

When define defines identifiers, don't add the (;) sign at the end.

# define defines macros

The # define mechanism includes a provision that allows parameters to be replaced into text, which is often referred to as a macro or define macro.

How macros are declared:

# define name (parament-list) stuff where parament-list is a comma-separated symbol table that may appear in stuff.

The left parenthesis of the parameter list must be close to the name. If there is a gap between the two, the parameter list is interpreted as part of the stuff.

# define SQUARE (x) x * x

# define replacement rules

1. When you call a macro, first check the parameters to see if they contain any symbols defined by # define. If so, they will be replaced first.

two。 The replacement text is then inserted into the location of the original text in the program. For macros, parameter names are replaced by their values.

3. Finally, scan the result file again to see if it also contains any symbols defined by # define. If so, repeat the above process.

Note:

1. Other variables defined by # define can appear in macro parameters and # define definitions. But for macros, there can be no recursion.

two。 When the preprocessor searches for symbols defined by # define, the contents of the string constant are not searched.

# and #

How do I insert a parameter into a string?

# include#define PRINT (n) printf ("the value of" # n "is% d\ n", n) int main () {int a = 7; PRINT (a); int b = 5; PRINT (b); return 0;}

# # function:

Macro parameters with side effects

When a macro parameter appears more than once in the definition of a macro, if the parameter has side effects, it may be dangerous when using the macro, leading to unpredictable consequences. The side effect is the permanent effect that occurs when the expression is evaluated.

Xbirthday 1 / without side effects / with side effects / side effects

To give an example:

Comparison between macros and functions

Macros are usually used to perform simple operations. For example, in finding the larger of the two numbers.

# define MAX (a, b) ((a) > (b)? (a): (B)

Why not use functions to accomplish this task? For the following reasons:

1. The code used to call and return from the function may take more time than it actually takes to perform this small calculation. So the macro function is better in terms of program size and speed.

two。 The parameters of the function must be declared as a specific type. So functions can only be used on expressions of the right type. This macro can be applied to shaping, long shaping, floating-point types, and so on. Macros are type-independent.

Comparison of macros and functions:

Naming convention

Generally speaking, the syntax for using functions and macros is very similar. So grammar itself can't help us distinguish between the two.

So it is generally: capitalize all macro names and not all function names.

# undef

This instruction is used to remove a macro definition.

# undef NAME / / if an existing name needs to be redefined, its old name must first be removed

For example:

Command line definition

Many C compilers provide a feature that allows programs to define symbols on the command line. Used to start the compilation process.

For example, this feature is useful when we want to compile different versions of a program based on the same source file. If a program declares an array of a certain length, if the machine memory is limited, we need a very small array, if the machine memory is larger, we need a larger array.

Conditional compilation

It is convenient to compile or discard a statement or a set of statements when compiling a program. You can use conditional compilation instructions.

Common conditional compilation instructions:

The file contains

The # include directive allows another file to be compiled.

This replacement method: the preprocessing deletes the instruction and replaces it with the contents of the containing file. If such a source file is included several times, it will actually be compiled several times.

How the header file is included:

Usually a large project will call multiple programs to merge, so how to avoid the repeated inclusion of header files and improve efficiency?

There are two common solutions:

1. The beginning of each header file is written:

# contents of ifndef _ _ TEST_H__#define _ _ TEST_H__// header file # endif

two。 Include this sentence before defining the header file:

# pragma once "C language program compilation and preprocessing example analysis" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report