C language pre-Compilation

Source: Internet
Author: User

 

1. Basic Content

The basic categories of pre-compiled commands are as follows:

Category

Command

Predefined symbols _ File _, _ line _, _ date _, _ time _, and _ stdc __
Macro # Define
File Inclusion # Include
Conditional compilation # If, # Elif, # else, # ifdef, # ifndef, # endif

The following table lists the commands, names, and functions:

Command Function
# Empty command
# UNDEF Remove an empty Definition
# Error Stop compilation and generate error messages
# Line Modify the values of _ line _ and _ file _
# Progma Allow the compiler to provide additional functions

When defining macros, there are twoOperator:

Operator Function
# Convert macro parameters to strings
## Concatenates multiple symbols into one identifier.
2. macro definition

1. Generally, no credit points are added at the end of the macro definition.

We need to add a semicolon when using it, just as we usually write statements.

2. Add brackets.

If there is an empty definition of a parameter, if it contains a numerical operation,Macro overallAndMacro Parameters"Brackets must be added to both ends.
For example: # define max (A, B) (a) + (B ));

3. Pay attention to spaces.

In macro definition with parameters, note the difference between "macro name" and "parameter list ".Space is not allowed.
For example, # define max (A, B) (a) + (B); there cannot be spaces between "Max" and "(a, B.

4. Do not use a parameter area with side effects to call macros.

Common Parameters with side effects include a ++ and getchar.
For example, if the macro is defined as # define max (A, B) (a) + (B), use max (I ++, J ++) to call the macro, this will increase the value of one of I or J by 2 instead of the expected 1.

5. You can use the compiler option to add or remove macros.

I am using GCC, the macro instruction is "-d", and the macro instruction is "-U ".

6. When macro parameters are replaced, the characters in the string are not replaced.

That is, the characters between double quotation marks are not replaced, and other characters are replaced, including those between single quotation marks.

7. Available # SetMacro parameter valueConvert to a string.

Directly use #.Macro parameter nameConvert to a string. Using the following technique (adding a transitional macro), you can convert the "macro parameter value" to a string (when the macro parameter has a value, the macro parameter is often a macro ).

# Include <stdio. h> # include <stdlib. h> # define Number Ten/* macro name is called number, macro value is ten */# define STR (x) # X # define xstr (x) STR (X) /* added a transition macro */INT main () {printf ("STR (number) = % s/n", STR (number )); printf ("xstr (number) = % s/n", xstr (number); System ("pause"); Return exit_success ;}

Output result:

Str(NUMBER) == NUMBER XStr(NUMBER) == ten

8. Use the # Operator to connect identifiers.

However,Not recommendedUse the operator # To connect the identifier, because it is easy to greatly reduce the readability of the program.

3. File Inclusion

1. Define the header file in the protection conditions.

The purpose isPrevent repeated header files. If you have checked the source code of GCC or other compilers, you must be familiar with this.

For example, if you want to write a header file,Myheader. h, Then the content form of your header file should be: (define a _ myheader macro)

# Ifndef _ myheader # DEFINE _ myheader 1/* contains your header file content */# endif/* _ myheader */

2. Note that the path symbols for Windows and Unix systems are different.

You can specify a path in # include to include the file, for example, # include "../head. H ". However, note thatBackslash "/"As the path separator, while the UNIX system usesSlash "/".

3. You can use the compiler option to set the search path.

The-Idir option used by GCC, for example,-I "d:/dev-CPP/include ".

4. Conditional compilation

1. # ifdef is equivalent to # If defined (), # ifndef is equivalent to # If! Defined ().

2. In# IfYou can use logical operators (&, | ,!). It cannot be used in # ifdef, which is also the advantage of # If.

# Include <stdio. h> # include <stdlib. h> # define a 1 # define B 0 int main () {# If defined (a) & defined (B) printf ("test logic operation in # If/N");/* If the logic above is true, a sentence is printed; if not, this statement will not be printed */# endif system ("pause"); Return exit_success ;}

Running result:

test logic operation in #if

3. sizeof (INT) is not evaluated in the pre-compilation phase.

As long as you know that the "pre-compilation stage" is before the real "compilation stage", it is easy to understand. The pre-compilation stage is only composedCharacters in source codeIn a sense, it sometimes does not even know what its operation object is. It only performs replacement according to the established rules.

Sizeof (INT), whether it is sizeof parsing or type parsing, is started in the "compilation stage", the compilation stage knows what its operation object is.

The following code is:ErrorOf

#if sizeof(int) == 2 printf("precompile sizeof(int)"); #endif
5. Additional Notes

Put a preprocessing commandWrite multiple linesTo useSymbol "/"And should be followed by the symbolLine Break. WhileNon-preprocessing commandsOfCode lineYou do not need to use this symbol to wrap the line.
Cause:Compilation phaseThe blank spaces are automatically ignored.Pre-compilation phaseNo.

 

Link to this article:Http://blog.csdn.net/daheiantian/archive/2011/02/09/6243993.aspx

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.