In-depth understanding of C language-04 preprocessing

Source: Internet
Author: User

When we mention preprocessing, we can't help but think of a classic problem.

Is the C language simple? Simple. The C language does not have much syntax, so I will soon learn it.

Indeed, the C language is much simpler without preprocessing. But once there is, the complexity increases by several levels.

Does C Support object-oriented programming? Yes. Preprocessing can help you implement it.

Does C language handle exceptions? Yes. Preprocessing can help you implement it.


Preprocessing is a Pandora box that can bring you to heaven or go to hell. (Joke)


When carefully studying the C language, we recommend that you download the C language specification:

C89 is widely used in the embedded field. Many of the tools provided by chip companies do not support C99.

C99


The specification is not used for viewing, but for querying. (Joke)


For the C language compiler code, see the following two:

TinyCC (http://bellard.org/tcc)

Gcc (the http://mirror.bjtu.edu.cn/gnu/gcc)


In actual projects, the following are the most used:


# Include file

Note: Do not be influenced by the C language teaching material, so that only. H files can be loaded. This is the largest mindset.

You can load any text file.

For example, the content of test. test is as follows:

Int myTestAry [] = {0, 1, 2, 3, 4, 5 };


# Include "test. test"


So what are the benefits?

The biggest benefit is that it can be combined with other languages.

For example, we can use Python to generate text that conforms to the C language syntax, such as font data and image data.


# Define

# Ifdef

# Undef

# If

# Endif

The Five Commands work together to control the version of the source code.

For example, the most common

# Ifdef WIN32/* if the current system is a WIN32 platform */

# Endif

Of course, the control variables defined by # define can also be replaced by-D variable name = variable value in the compilation script.


# Define also has a common function of defining constants.

# Define PI 3.1415926


However, these are all for small purposes.

What is really powerful is to use # define to build a complex mechanism. Unfortunately, the project is basically disabled for readability.

If you are interested, you can search for the C language and object-oriented C language and Exception Handling C language and template, or check the Macros in MFC.

This is a painful and happy feature.


In actual projects, the most commonly used is to print Log statements.

For example, tracking function execution:

# Define FUNC_BEGIN () do {printf ("Enter % s % d \ n", _ FUNCTION __, _ LINE _);} while (0)

# Define FUNC_END () do {printf ("Leave % s % d \ n", _ FUNCTION __, _ LINE _);} while (0)


Add

FUNC_BEGIN ();

Add:

FUNC_END ();


More complex print statements:

# Define MYPRINT (...) do {printf (_ VA_ARGS _);} while (0)

_ FUNCTION __: current FUNCTION name, determined by the compiler

_ LINE __: current code LINE number

_ VA_ARGS __: Variable Parameter


# Error

Compilation error. You can add the following statement in the Code for testing. ()

# Error "not supported"


# Pragma

Controls the compiler, which is related to the implementation of the compiler.

In actual projects, it is not used much for portability.

For specific projects, we recommend that you refer to the compiler instructions before using them and describe them in project risk management. (This is important and makes sense for cross-platform project porting)


All in all, preprocessing is a place where the compiler shows itself and makes everyone crazy and lost.

For developers, this is heaven, and for project managers, this is hell. ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.