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

C language macros define how to use them

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

Share

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

This article mainly explains "how to use the macro definition of C language". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the macro definition of C language.

I. the "defects" of functions in C language

There is only value passing between the argument and the parameter, so the argument cannot be changed directly in the function.

Second, understand the function again

Function is a means of code reuse

Encapsulate a piece of code that implements a function (as a whole)

Give the code snippet an appropriate name (use the code by name)

Define parameters (define the issues that the code snippet needs to deal with)

III. Macros in C language

Macro is a supplementary way of code reuse in C language.

Macro definition syntax: # define MACRO (param) code_segment

Syntax for macros: MACRO (num)

For example:

# define ADD (an int b) a + bint main () {int z = ADD (1); printf ("z =% d\ n", z); return 0;} IV. Differences between macros and functions

Macros are not functions. There is no procedure for function calls to use macros.

The function call first passes the parameter value, then jumps to the execution function body, and finally returns

Using macros is simply "code copy and paste" and then replaces parameters

The same function executes the same function body code no matter how many times it is called

The same macro will be "copied and pasted" the same code each time it is used

V. A brief introduction to the composition of the compiler

Preprocessing module: handles all macros and statements that begin with # (copy, paste and replace)

Compiler module: translating C programs into binary programs

Link module: assemble binary programs into executable programs

6. examples of the use of macros

Take a simple exchange of values of two numbers as an example:

# include # define SWAP (a, b) {int t = a; a = b; b = t;} int main () {int x = 1; int y = 2; SWAP (x, y); / / {int t = x; x = y; y = t;} printf ("x =% d, y =% d\ n", x, y); return 0;}

The following is the output:

VII. Further discussion on macro constant

# define NAME value

The preprocessing module replaces the NAME identifier that appears in the code with value

Therefore, macro constants are essentially the same as literals (real constants)

Thank you for your reading. the above is the content of "how to use the macro definition of C language". After the study of this article, I believe you have a deeper understanding of how to use the macro definition of C language. the specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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