Compilation preprocessing and dynamic storage allocation (1)

Source: Internet
Author: User

1 compilation preprocessing

In C, any line that begins with the "#" number is called the "compile preprocessing" command line. The pre-processing commands for the C language are: #define, #undef, #include, #if, #else, #elif, #endif, #ifdef, #ifndef, #line, #pragma, #error.

1.1 Macro Replace 1.1.1 macro definition with no parameters

(1) macro definition with no parameters The command line form is as follows:

Replace text #define Macro name

Spaces are separated by a space between # define, macro name, and macro replacement text. For example:

#define SIZE 100

(2) The replacement text can contain a defined macro name, for example:

#define PI 3.14

#define ADDPI (PI + 1)

#define TWO_ADDPI (2 * addpi)

(3) When the macro definition does not fit in one row, you need to add a backslash "\" immediately after the last character, if you want to continue on the next line. For example:

#define LEAP_YEAR Year% 4 = = 0\

&& Year% 100! = 0 | | Year% 400 = = 0

If there are spaces before "\" or at the beginning of the next line, these spaces are also added when the macro is replaced.

(4) The same macro name cannot be defined repeatedly unless the two macro definition command line is exactly the same.

(5) Replacement text cannot replace a string with the same macro name in double quotation marks. For example, if YES is a defined macro name, you cannot replace the Yes in printf ("yes") with the replacement text associated with it.

(6) The replacement text does not replace the component in the user identifier. For example, the macro name Yes does not replace Yes in the identifier Yesorno.

(7) Identifiers used as macro definitions are usually expressed in uppercase letters, which is not a syntactic rule, but a habit that distinguishes it from other identifiers in the program.

(8) In C program, the definition of macro definition is generally written at the beginning of the program.

1.1.2 Macro Definition with parameters

(1) macro definition with parameters command line form is as follows:

Replace text #define Macro name (formal parameter list)

For example:

#define MU (x, y) ((x) * (y))

Above the macro definition command line, mu (X, Y) is referred to as "macro", MU is the user identifier, called the macro name. The macro name and the opening parenthesis "(" must be next to each other, they shall not have spaces between them.) Where a pair of parentheses consists of several identifiers called formal parameters, separated by commas between the parameters. The replacement text should contain the tangible parameters.

(2) Same as macro definition without parameters, the same macro name cannot be defined repeatedly unless the two macro definition command line is exactly the same.

(3) when calling a macro name with parameters, a pair of parentheses is necessary, the number of arguments in parentheses should be the same as the number of formal parameters, if there are more than one parameter, they are separated by commas. At precompilation time, the compilation preprocessor replaces the macros with replace text and replaces the parameters in the replacement text with the corresponding arguments.

(4) The formal parameters and the entire expression in the "replacement text" should be enclosed in parentheses, for example:

#define MU (x, y) ((x) * (y))

(5) There are similarities between macro substitution and function invocation, but there is no type requirement for parameters in macro substitution. For example, a call to MU can be either a product of two integers or a product of two real numbers. If you are calling a function to multiply two numbers, you need to define different functions for different types of parameters.

(6) The macro substitution is done by the preprocessor before compiling, so the macro substitution does not occupy the elapsed time. function calls are made when the program is run, and processing time is required during function calls.

1.1.3 terminating a macro definition

The scope of the macro definition can be terminated prematurely with #undef. For example:

#define PI 3.14

Main ()

...

#undef PI

...

The above pi scope starts at the # define PI 3.14 command line and ends at the #undef Pi command line. PI becomes undefined after #undef, no longer represents 3.14.

1.2 file contains

C Language Development program, you can put some macro definitions in separate files according to the function. When you need to use a type of macro definition, you do not need to redefine it in your program, as long as you include the file where the macro definition resides at the beginning of the program.

The so-called file contains, refers to in one file to contain the entire content of another file. The C language uses the # include command line to implement the functionality that the file contains. The #include command line is in the following form:

#include "file name"

Or

#include < file name >

When precompiled, the precompiled program replaces this command line with the contents of the specified file. A. If the file name is enclosed in double quotation marks, the system first finds the specified include file in the same directory as the source program, if it is not found, and then finds it in the relevant directory according to the standard mode specified by the system; B. If the file is enclosed in angle brackets, the system will go directly to the directory that is specified by the system.

Description

(1) The # include command line that contains the file should usually be written at the beginning of the source program file used, so the include file is also referred to as the "header file". The header file name can be specified by the user, and the suffix does not necessarily use ". h".

(2) Include files that typically contain some common # define command lines, external instructions, or prototype descriptions of library functions. For example, stdio.h is such a header file.

(3) When the included file is modified, the source program that contains the file must be re-compiled to connect

(4) In one program, any number of # include command lines are allowed.

Compilation preprocessing and dynamic storage allocation (1)

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.