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

The method of pre-compilation in C language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "the method of pre-compilation of C language". In the operation of actual cases, many people will encounter such a dilemma, so 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!

I. predefined symbols

Predefined symbols are defined by the system itself:

The location of the source file compiled by FILE

The current line number of the LINE file

The date the DATE file was compiled

The time when the TIME file was compiled

STDC if the compiler follows ASNSI C, its value is 1, otherwise it is not defined

II. # define definition identifier

Syntax: # define name stuff (replace name with stuff)

# define MAX 100 # define STR "hehe" int main () {int max = MAX; printf ("% d\ n", max); / / output 100 printf ("% s\ n", STR); / / output hehe return 0;} III, # define definition macro

The # define mechanism includes a mechanism that allows parameters to be replaced into text, an implementation often referred to as a macro or macro definition

The way macros are declared: # define name (parament-list) stuff where parament-list is a comma-separated symbol table, they may appear in stuff.

Note: the left parenthesis of the parameter list must be adjacent to the name, and if there is any space between the two, the parameter list will be interpreted as part of the stuff.

# define SQUARE (X) X*Xint main () {int ret = SQUARE (5); printf ("% d\ n", ret); / / output 25 return 0;}

There is something wrong with the macro definition code above: if we change a parameter (change 5 to 5 by 1), the output is not 36 but 11?

# define SQUARE (X) X*Xint main () {int ret = SQUARE (5x1); / / after replacement is (5x1x5x1 = 11) printf ("% d\ n", ret); / / output 11 return 0;}

No parentheses

Therefore, macro definitions used to evaluate numeric expressions should be parenthesized in this way to avoid unpredictable interactions between operators in parameters or adjacent operators when using macros.

The correct code:

# define SQUARE (X) (X) * (X) int main () {int ret = SQUARE (5x1); printf ("% d\ n", ret); / / output 36 return 0;} IV, # define replacement rules:

One, # define NAME "lisa"

There is a "NAME" in the program, but the contents of the "" will not be replaced by macros.

Second, the one before the macro definition must be a legal user identifier

Third, the macro definition does not mean that the following things can be written casually, and the two "" of the string cannot be taken apart.

Four: # define NAME "lisa"

There is the above macro definition in the program, and there is a sentence in the program:

In this case, NAMELIST will not be replaced by "lisa" LIST

Fifth, macros cannot be recursive.

Five, # and # symbols (rare)

The purpose of #: insert a parameter into a string

If we want to implement a code that inserts parameters into a string using "#"

Here, the parameter aforme b is inserted into the string.

The role of # #: you can combine symbols on both sides of it into one symbol, which allows macro definitions to create identifiers from separate pieces of text.

The three lines of code in the figure are equivalent:

Printf ("% d\ n", AGE (lisa,24))

Printf ("% d\ n", AGE (lisa##24))

Printf ("% d\ n", AGE (lisa24))

6. Comparison between macros and functions

Both functions and macros can find the maximum of two numbers.

/ / function int Max (int x, int y) {return (x > y? X: y);} / Macro # define MAX (XMagi Y) ((X) > (Y)? (X): (Y)) int main () {int a = 10; int b = 20; int max = Max (a, b); / / output 20 printf ("% d\ n", max); max = MAX (a, b); printf ("% d\ n", max) / / output 20 return 0;}

It is better to implement macros than functions by analyzing the above code for two reasons:

The code used to call and return from a function may take more time than the small computing work actually performed, so macros are better than functions in terms of program size and speed.

The parameters of the function must be declared to a specific type. Therefore, the function can only be used in the expression tense of the appropriate type. Macros, on the other hand, are type-independent.

Of course, macros also have disadvantages compared to functions:

Each time you use a macro, a copy of the code defined by the macro inserts the replacement into the program. Unless the macro is relatively short, it is possible to greatly increase the length of the program.

Macros cannot be debugged

Macros are not rigorous enough because they are type-independent.

Macros can cause problems with operator precedence, making the program error-prone.

# define defines a comparison table between macros and functions

Attribute # define defines the length of the macro function code is inserted into the program every time it is used, except for very small macros, the length of the program increases significantly, and the code of the function appears only in one place, and every time you use this function, the same code in that place is called faster, which has the function call and the extra overhead of the function. So relatively slow operator priority macro parameters are evaluated in the context of all surrounding expressions, unless parentheses are added, otherwise the priority of adjacent operators may produce unpredictable results, so it is recommended that macros use parentheses when writing function parameters are evaluated only once when the function is called, and its result value is passed to the function. The result of the evaluation of the expression is easier to predict. Parameter parameters with side effects may be replaced in multiple positions in the band macro, so parameter evaluation with side effects may produce unpredictable results. Function parameters are evaluated only once when parameters are passed. As a result, it is easier to control that the parameters of parameter type macros are independent of type. As long as the operation on parameters is legal, it can be applied to any parameter type function whose parameters are related to type, if the type of parameters is different. Different functions are needed, even if they perform different debugging tasks and recursive macros are not convenient for debugging. Recursive functions can be debugged sentence by statement and can be recursive.

Naming convention: capitalize all macro names and not all function names.

7. # undef

# undef directive to remove a macro definition

When # undef removes the macro definition, use the error message again. As shown in the figure:

This is the end of the content of "the method of pre-compilation of C language". Thank you for your 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