Macro-definition Technique of C language-contains different header files multiple times.

Source: Internet
Author: User

Macro-definition Technique of C language-contains different header files multiple times.

1. the header file is defined as follows:

/* Declears in "funcs. h "*/

FUNC_1 (id_fun0000001)

FUNC_1 (id_fun0000002)

FUNC_2 (ID_FUN2_001)

FUNC_2 (ID_FUN2_002)

 

2. Multiple include header files

# Define FUNC_1 (opt) opt,

# Define FUNC_2 (opt)

Enum aaa {

# Include "funcs. h"

ID_FUN1_END

};

# Undef FUNC_1

# Undef FUNC_2

 

# Define FUNC_1 (opt)

# Define FUNC_2 (opt) opt,

Enum bbb {

# Include "funcs. h"

ID_FUN2_END

};

# Undef FUNC_1

# Undef FUNC_2

 

In this way, after macro translation, we define the two enum as follows:

Enum aaa {

Id_fun0000001,

Id_fun0000002,

ID_FUN1_END

};

 

Enum bbb {

ID_FUN2_001,

ID_FUN2_002,

ID_FUN2_END

};


(C) Define the macro to prevent repeated inclusion of this header file. What is this sentence?

When one C language program is composed of multiple source program files, because each source program file has # include <...>
# Include "... h". In the end, they may form cross-inclusion and Repeated inclusion, resulting in errors.

To avoid this situation, you can use the fixed macro method to wrap each header file.

At the beginning of the formal compilation, the compiler now processes macros, that is, the "pre-compilation command ".

Use a macro (line starting with #) to package the header file content:
# Ifndef _ HEADER_One_H _ // indicates that the macro starts the row. If _ HEADER_One_H _ is not defined, it enters; otherwise, it exits.

# Define _ HEADER_One_H _ // define _ HEADER_One_H _//

Header1.h
Header file content

# Endif // macro end line

In C language, what are the differences between preprocessing File Inclusion (# include) and macro definition (# define? Yes

# Include is used to call included header files.
# Define is a defined macro, such as a constant.

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.