C + + preprocessing commands

Source: Internet
Author: User

C preprocessing command

A What is preprocessing command

In short, preprocessing is to improve the efficiency of programming, before compiling the program, note that before compiling, according to the preprocessing command to deal with the corresponding. Preprocessing commands include: #define, #include, #ifdef, #endef, D, etc. For example, if you define a symbol constant A with the #define command, all the A in this file will be substituted for the specified string when preprocessing.

After preprocessing, the program will no longer contain the preprocessing statements. Then the usual compilation process is done. This is also the important difference between C language and other languages.

C language provides the following three kinds of preprocessing functions:

1. Macro definition

2. The document contains

3. Conditional compilation

These features are implemented by preprocessing commands, typically to distinguish C statements, which begin with a symbol #.

Two Specific preprocessing commands

A Macro definition

A macro definition is simply to define a identifier that represents a global variable, or a statement. When you meet the identifier later, the program replaces it with the corresponding variable or statement.

For example:

#define PI 3.1415926

#include <stdio.h>

Int Main ()

{

Printf ("%f", PI);

}

Then the program will output 3.1415926

The definition of a macro is roughly divided into: a macro definition with no parameters, two macro definitions with parameters:

A. Macro definition with no parameters:

# define Designator string

For example, the #define PI 3.1415926 in the previous example. In the precompilation is to replace the macro name with a string to become "macro expansion", and the #define command when the macro defines the command, PI is the macro name, but also the meaning of the indicator.

Description

(1) Macro definitions generally used in capital letters, easy to identify

(2) The use of a macro name instead of a string, you can reduce the amount of work to write some strings, and modify the string is also easy to replace the only need to modify the macro name instead of the string on it.

(3) The macro definition is not C language, it does not need to be added;

(4) #define命令出现在程序函数外面, the valid range is this document.

(5) You can use the #undef command to manually terminate the scope of a macro.

For example:

#include <stdio.h>

#define PI 3.1415926

int main ()

{

printf ("%f", PI);

return 0;

}

#undef PI

int Fundef ()

{

return PI;

}

At this time the function of the subsequent compile will be an error, said PI variable no declaration.

(6) in making a macro definition, you can refer to the defined macro name and layer substitution.

Let's give you an example:

For example:

#define PI 3.1415926

#define R 2

#define L 2*pi*r

#define S Pi*r*r

This is a reference to a defined macro that implements a permutation.

(7) If the program has double brackets around the same string and macro name, the program will not be replaced, because the macro definition is a proper noun, and do not participate in the compilation of the program, so do not allocate memory space.

1. Macro definition with parameters

Macro definition with parameters in short, there are parameters in the macro definition, in general form:

#define Macro Name (parameter) string

For example:

#define S (A,B) a*b

Description

(1) The expansion of a macro with a parameter replaces the formal parameter in the #define command line only with the argument characters in parentheses following the macro name in the statement.

(2) When the macro is defined, do not add a space between the macro name and the argument.

(3) Predefined macro parameters are not allowed to define the type, because only replace

(4) Calling a function can only get a return value, and a macro can get several results

For example:

#define CIRCLE (r,l,s,v) l=2*pi*r; S=pi*r*r; V=4/3*pi*r*r*r

float r,l,s,v;

scanf ("%f", &r);

CIRCLE (R,L,S,V);

printf ("Round circumference is:%f\n", L);

printf ("Area%f\n", s);

printf ("Volume%f\n", V);

(5) When a macro expands, it is replaced each time, so using a macro definition with parameters will make the program longer, relative to the function. However, the macro substitution does not occupy the Run-time, only the compilation time, while the function call takes up the running time (including allocation Unit, protection field, value Pass, return, etc.)

Two file contains processing

File inclusion processing means that a source file can include the entire contents of another source file, so that the contents of another file can be used. The C language provides a #include command to implement.

General form:

#include "file name"-----Multiple files for customization

#include < file name > multiple for system-defined files.

For example:

Predefined macro with parameters in the #include<stdio.h> of our output statement

is a system-defined class library that contains statements for input and output

Description

(1) A #include command can contain only one file

(2) #include命令可以传递, that is to say, if there are three files. file1.c file2.c file3.c joins file12.c file containing file1.c, FILE3.C also contains file2.c

(3) The contained file and the file in which it is located are compiled into one file after compiling.

Three Conditional compilation

In general, the source program is known as the line to see the compilation, but sometimes want a part of the program only to meet certain conditions to compile, that is, to this part of the content to specify the compilation conditions, is "conditional compilation"

Conditional compilation Command form:

(1) #ifdef designator

Program Section 1

Else

Program Section 2

#endef

Meaning: If the identifier is already defined, compile the program segment 1, or compile the program segment 2

(2) #ifdef designator

Program Section 1

#endef

Meaning: If the identifier is already defined, compile the program segment 1.

Description

Conditional compilation can be used entirely instead of, but conditional compilation is due to the use of if all statements are compiled, the if to be judged, the long running time, and conditional compilation, you can reduce the compiled statements, reduce program length and run time.

Finally, in addition to these precompilation, there are more complex precompiled commands such as #pragma.

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.