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

How to use C language pre-compilation

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

Share

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

This article mainly introduces the relevant knowledge of "how to use C language pre-compilation". The editor shows you the operation process through actual cases, and the operation method is simple, fast and practical. I hope this article "how to use C language pre-compilation" can help you solve the problem.

1. Built-in symbol

These symbols can be used directly:

_ _ FILE__ dot c file full name

_ _ LINE__ current line number

_ _ DATE__ compilation date

_ _ TIME__ compile time

For example:

# includeint main () {printf ("full name of file:% s\ n", _ _ FILE__); printf ("current run line number:% d\ n", _ _ LINE__); printf ("compile date:% s\ n", _ _ DATE__); printf ("compile time:% s\ n", _ _ TIME__); return 0;}

Effect:

2. Custom symbols

# define name value

Without a semicolon. In the precompilation phase, all names are replaced by values.

Example: give printf a different name.

# include#define say printfint main () {say ("Hello, World!\ n"); return 0;}

Effect:

Example: customize an endless loop symbol.

# include#define loop while (1) int main () {int I = 0; loop {if (I > 1000) {break;} else {printf ("Hello, World! \ t% d\ n ", I); iTunes;}} return 0;}

Effect:

3. Custom macros

It's pretty much the same as a custom.

It is equivalent to a function, but it is executed at the stage of precompilation.

And replace it.

# define name (parameter list) expression

For example: output string.

# include#define say (s) printf (s) int main () {say ("Hello, World!\ n"); return 0;}

Effect:

Replacement logic: recursion.

To see if the parameters have define, there are nesting dolls.

Until not, replace yourself.

Symbols inside the string are ignored.

4. Conditional compilation

You can use # define to define a symbol that represents whether or not to execute.

# include#define _ _ DEBUG__ 1int main () {if (_ _ DEBUG__) {printf ("debug mode on\ n");} else {printf ("debug mode off\ n");} return 0;}

Effect:

At the same time, you can let this code skip compilation directly.

# include#define _ _ DEBUG__ 1int main () {# if _ DEBUG__ printf ("debug mode on\ n"); # endif return 0;}

This way of writing also has matching branches of choice.

# include#define _ _ DEBUG__ 0#define _ _ WARN__ 1int main () {# if _ _ DEBUG__ printf ("debug mode\ n"); # elif _ _ WARN__ printf ("warning mode\ n"); # else printf ("normal mode\ n"); # endif return 0;}

Effect:

This is the end of the introduction of "how to use C language pre-compilation". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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