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 preprocessing in C language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the sample analysis of preprocessing in C language, I believe that most people do not know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's go and learn about it!

# define defines macro parameters with side effects

Let's take a look at the following code

The results are 12, 11, 13, respectively.

When the parameter is replaced, we first judge the expression (axiom +) > (bounded +)?, then the value of a plus the value of 1b plus 1, and then execute the expression (bounded +) at this time, the value of b is 12, and the value of b after execution is 1, then the value of an is 11, and the value of b is 13. It can be seen that macros in this case have side effects and may lead to unpredictable results.

# advantages of define defining macros

To implement the following code, is it better to use a function or a macro?

1. It may take more time to call functions and return code than this mini-calculation, so macros are better in speed and scale.

two。 The parameters of the function must be declared as a specific type, so the function can only be used in expressions of the appropriate type, while macros are independent of the type, so there will be a warning if you use the function when comparing floating-point types

# define defines macro disadvantages

1. Each time a macro is used, a piece of code defined by the macro is inserted into the code, and the length of the code is greatly increased unless the macro is shorter.

two。 There is no way to debug macros because of poor information. What we see is the name of the macro definition, and the actual run is the code of the macro definition. Example:

It looks like the code is right, but in fact, the error cannot be found because the macro cannot be debugged.

3. Macros are not rigorous enough because they have nothing to do with type

4. Macros may cause problems with operator precedence, which can easily lead to errors in the results.

Preprocessing predefined symbol

_ _ FILE__ compiled source file

_ _ date when the DATE__ file was compiled

_ _ time when the TIME__ file was compiled

Current line number of _ _ LINE__ file

_ _ STDC__ is 1 if the compiler follows the ANSI C standard, otherwise it is not defined

Preprocessing instruction

# define Macro definition

# undef revokes the defined macro name

# include causes the compiler to embed another source file into the source file with # include

# ifdef if defined

# ifndef if not defined

# endif

The preprocessing instructions are those above and other instructions that begin with #

Conditional compilation

You can let the statement choose to compile or discard

Often used for debug code, delete unfortunately, retain and get in the way, or prevent repeated header files from being compiled multiple times

1. Debug code

If not defined, the code in it will not be compiled

two。 Prevent duplicate header files from being compiled multiple times

You can also use # pragma once

The above is all the content of this article "sample Analysis of preprocessing in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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