Compilation preprocessing and Dynamic Storage Allocation (1), compilation preprocessing

Source: Internet
Author: User
Tags integer numbers

Compilation preprocessing and Dynamic Storage Allocation (1), compilation preprocessing
1. Compile preprocessing

In C language, all lines starting with "#" are called "compile preprocessing" command lines. C-language pre-processing Commands include: # define, # undef, # include, # if, # else, # elif, # endif, # ifdef, # ifndef, # line, # pragma, # error.

1.1 macro replacement 1.1.1 macro definition without Parameters

(1) The macro-defined command line format without parameters is as follows:

# Define macro name replacement text

Separate # define, macro name, and macro replacement text with spaces. For example:

# Define SIZE 100

(2) The replacement text can contain the defined macro names, for example:

# Define PI 3.14

# Define ADDPI (PI + 1)

# Define TWO_ADDPI (2 * ADDPI)

(3) When the macro definition cannot be written in a row and needs to be continued in the next row, you only need to add a backslash "\" after the last character. For example:

# Define LEAP_YEAR year % 4 = 0 \

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

If spaces are left before "\" or at the beginning of the next line, these spaces will be added during macro replacement.

(4) The same macro name cannot be defined repeatedly unless the two macro definition command lines are completely consistent.

(5) text replacement cannot replace strings with the same macro name in double quotation marks. For example, if YES is a defined macro name, you cannot replace YES in printf ("YES") with the replacement text related to it.

(6) text replacement does not replace the components in the User Identifier. For example, if the macro name is YES, YES in YESORNO is not replaced.

(7) The identifier used as a macro definition is usually expressed in uppercase letters. This is not a syntax, but a habit to distinguish it from other identifiers in the program.

(8) In the C program, macro-defined positions are generally written at the beginning of the program.

1.1.2 macro definition with Parameters

(1) the syntax of macro-defined command lines with parameters is as follows:

# Define macro name (parameter table) Replace text

For example:

# Define MU (X, Y) (X) * (Y ))

In the macro definition command line above, MU (X, Y) is short for "macro", and MU is the User Identifier, called the macro name. Macro name and left brace "(" must be placed next to each other, and no space is left between them. A pair of parentheses is composed of several identifiers called form parameters. Each form parameter is separated by a comma. Replace text should contain parameters.

(2) The same macro definition is the same as that without parameters. The same macro name cannot be defined repeatedly unless the two macro definition command lines are completely consistent.

(3) When a macro name with parameters is called, a pair of parentheses is required. The number of parameters in parentheses should be the same as the number of parameters. If there are multiple parameters, they are separated by commas. During pre-compilation, the pre-Compilation Program replaces the macro with "Replace text" and replaces the form parameters in "Replace text" with the corresponding real parameters.

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

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

(5) macro replacement is similar to function call, but in macro replacement, there is no type requirement for parameters. For example, you can call MU to obtain both the product of two integer numbers and the product of two real numbers. If a function is called to calculate the product of two numbers, different types of parameters need to be defined.

(6) macro replacement is completed by the Preprocessing Program before compilation. Therefore, macro replacement does not take up the running time. Function calls are performed when the program is running, and the processing time is required during the function call process.

1.1.3 terminate macro definition

You can use # undef to terminate the macro-defined scope in advance. For example:

# Define PI 3.14

Main ()

...

# Undef PI

...

The above PI scope starts from the # define PI 3.14 command line and ends with the # undef PI command line. After # undef, the PI becomes undefined and no longer represents 3.14.

1.2 File Inclusion

When developing a program in C language, you can save some macro definitions to different files by function. When you need to use a certain macro definition, you do not need to re-define the macro definition in the program, but you just need to include the file containing the macro definition at the beginning of the program.

A file contains all the content of another file. The C language uses the # include command line to implement the file inclusion function. # The include command line format is as follows:

# Include "file name"

Or

# Include <File Name>

During pre-compilation, the pre-Compilation Program replaces this command line with the content in the specified file. A. if the file name is enclosed by double quotation marks, the system first searches for the specified contained file in the directory where the source program is located. If the file cannot be found, it searches for the file in the relevant directory according to the standard method specified by the system; B. if the file is enclosed in angle brackets, the system will directly search for the Directory according to the standard method specified by the system.

Note:

(1) The # include command line containing files should usually be written at the beginning of the source program file used. Therefore, the contained files are also called "header files ". The header file name can be specified by the user, and its suffix is not necessarily ". h ".

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

(3) After the inclusion file is modified, the source program containing the file must be re-compiled and connected.

(4) a program can contain any number of # include command lines.

Example 1: Define the macro MYALPHA (c) with parameters to determine whether the character is 1 # include <stdio. h> 2 // macro definition determines whether the input character is a letter 3 # define MYALPHA (c) (c> = 65 & c <= 90) | (c >=97 & c <= 122) \ 4? 1: 0) 5 int main () 6 {7 char ch; 8 9 printf ("Please enter a character 'C '. \ n "); 10 printf (" Enter 'f' or 'F' to finish. \ n "); 11 12/* 13 scanf will read the linefeed in. To prevent the linefeed from being read, use the modifier '* '14: does not assign the input item to the corresponding variable. For example: 15 scanf ("% c % * c % c", & cha, & chb); 16 when input: m n q, Assign m to the variable cha, n Skip. q is assigned to the variable chb17 */18 scanf ("% c % * c", & ch); 19 20 while (ch! = 'F' & ch! = 'F') 21 {22 printf ("The ASCII code of character is: % d \ n", ch); 23 24 if (MYALPHA (ch )) 25 {26 printf ("c is a character. \ n "); 27} 28 else29 {30 printf (" c is not a character. \ n "); 31} 32 33 scanf (" % c % * c ", & ch); 34} 35 return 0; 36} 37 # undef MYALPHA // terminate macro definition scopeMYALPHA

 

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.