C-language pre-processing procedures and comments

Source: Internet
Author: User
C-language pre-processing programs and comments-general Linux technology-Linux programming and kernel information. The following is a detailed description. The source code of the C program may include various compilation commands, which are called preprocessing commands. Although they are not actually part of the C language, they extend the C programming environment. This section describes how to use preprocessing programs and annotations to simplify the program development process and improve the readability of the program.
4.7.1C Preprocessing Program
The C language Preprocessing Program defined by ANSI includes the following commands:
# Define
# Error
# Include
# If
# Else
# Elif
# Endif
# Ifdef
# Ifndef
# Undef
# Line
# Pragma
Obviously, all the pre-processing commands start with a symbol #, which is described below.
4.7.2 # define
Command # define defines an identifier and a string. Each time This identifier is encountered in the source program, it is replaced by a defined string. The ANSI standard defines the identifier as a macro name, and the replacement process is called macro replacement. The command is generally in the following format:
# Define identifier string
Note that this statement does not have a semicolon. There can be any space between the identifier and the string. Once the string starts, it ends only with a new row.
For example, if you want to set true to 1 or FALSE to 0, two macros # define
# Define TURE 1
# Define FALSE 0
This means that 0 or 1 is used instead of true or FALSE in the source program.
For example, print "012" on the screen ":
Printf ("% d", FALSE, TRUE, TRUE + 1 );
After the macro name is defined, it can become part of other macro name definitions. For example, the following code defines the values of ONE, TWO, and THREE.
# Define ONE 1
# Define two one + ONE
# Define three one + TWO
It is important to understand that macro replacement is just a string instead of an identifier. Therefore, if you want to define a standard error message, you can write the following code:
# DefineE_MS "standard error on input \ n"
Printf (E_MS );
When the Compilation Program encounters the identifier E_MS, it should be replaced with "standard error on input \ n. For compiled programs, the printf () Statement is actually in the following format:
Printf ("standard error on input \ n ;")
If the string contains an identifier, It is not replaced. For example:
# Define XYZ this is a test
.
.
.
Printf ("XYZ ");
This section does not print "this is a test" and prints "XYZ ".
If the string is longer than a row, you can use a backslash to continue the row at the end of the row. For example:
# DefineLONG_STRING "this is a very long \
String that is used as an example"
C language programs generally use uppercase letters to define identifiers. This Convention allows a person to quickly find out where there is a macro replacement when reading the program. It is best to put all the # define statements in the beginning of the file or an independent file (accessed with # include), rather than spreading them to the entire program.
The most common purpose of Macro Substitution is to define the constant name and the number of games in the program ". For example, a program defines an array, and its subprograms need to access the array, and should not directly set the array size with constants, it is best to define it by name (the array size needs to be changed ).
# Define MAX_SIZE100
Float balance [MAX_SIZE;]
# Another useful feature of the define command is that macro names can take parameters. Every time you encounter a macro name, the parameters connected to it are replaced by the real parameters in the program. For example:


(400) {this. resized = true; this. width = 400; this. alt = 'click here to open new window';} "onmouseover =" if (this. resized) this. style. cursor = 'hand'; "onclick =" window. open ('HTTP: // www.pcvz.com/Program/image/200602/2006210162547859.GIF'); ">

When the program is compiled, the expressions defined by MIN (a, B) are replaced, and x and y are used as the operands, that is, the printf () Statement is replaced with the following form:
Printf ("the minimum is: %, d" (x
Both lines of code use the C compiler to read and compile subprograms used to process Disk File libraries.
Embedding a file in the # include command is feasible. This method is called a nested embedded file. The nested hierarchy depends on the specific implementation.
If the explicit path name is part of the object identifier, search for the embedded object in only the subdirectories. Otherwise, if the file name is enclosed in double quotation marks, the current working directory is retrieved first. If no file is found, search all directories described in the command line. If no file is found, search for the standard directory defined during implementation.
If there is no explicit path name and the file name is enclosed by Angle brackets, it is first retrieved in the directory of the compiled command line.
If the file is not found, the standard directory is retrieved and the current working directory is not retrieved.
4.7.5 Conditional compilation command
There are several commands to selectively compile each part of the program's source code. This process is called Conditional compilation. Commercial software companies widely use Conditional compilation to provide and maintain many customer versions of a program.
1. # if, # else, # elif and # endif
# The general meaning of if is that if the constant expression after # if is true, the code between it and # endif will be compiled; otherwise, the code will be skipped. Command # endif identifies the end of a # if block, see Example 4-13.
# Ifconstant-expression
Statement sequence
# Endif


(400) {this. resized = true; this. width = 400; this. alt = 'click here to open new window';} "onmouseover =" if (this. resized) this. style. cursor = 'hand'; "onclick =" window. open ('HTTP: // www.pcvz.com/Program/image/200602/2006210162547809.GIF'); ">

Because MAX is greater than 99, the above program displays a string of messages on the screen. This example illustrates an important point: the expression following # if is evaluated during compilation. Therefore, it must contain only constants and defined identifiers and cannot use variables. The expression does not contain the sizeof operator.
# The else command functions a bit like the else in C; # else creates another option (in the case of # if failure ). The preceding example can be expanded. See example 4-14.


(400) {this. resized = true; this. width = 400; this. alt = 'click here to open new window';} "onmouseover =" if (this. resized) this. style. cursor = 'hand'; "onclick =" window. open ('HTTP: // www.pcvz.com/Program/image/200602/2006210162547551.GIF'); ">

In this example, because m a x is smaller than 9 9, The # if block is not compiled, but the # else block is compiled. Therefore, the screen displays
Displays the message "compiled for small array.
Note that # else is both a # if block and a # else block header. This is because any # if has only one # endif.
# The elif command has the same meaning as the else if command. It forms an if else-if step-by-step statement and supports multiple compilation options.
# Elif followed by a constant expression. If the expression is t r u e, the code block after compilation is not followed by other # elif expressions.
Line test. Otherwise, test the next part in sequence.
# If expression
Statement sequence
# Elif expression1
Statement sequence
# Elif expression2
Statement sequence
# Elif expression3
Statement sequence
# Elif expression4
# Elif expression3N
Statement sequence
# Endif
For example, the following program uses ACTI Ve _ COUNTRY to define currency symbols.
# Define US 0
# Define ENGLAND1
# Define FRANCE 2
# Define ACTIVE_COUNTRY US
# If ACTIVE_COUNTRY = US
Char currency [] = "dollar ;"
# Elif ACTIVE_COUNTRY = ENGLAND
Char currency [] = "pound ;"
# Else
Char currency [] = "franc ;"
# Endif
# If and # elif commands may be nested to the required permissions, where # endif, # else, or # elif is associated with the latest # if or # elif. For example, the following program is completely valid.
# If MAX> 100
# If SERIAL_VERSION
Int port = 198;
# Elif
Int port = 200;
# Elif
# Else
Char out_buffer [100];
# Endif
2. # ifdef and # ifndef
Another method of Conditional compilation is to use the # ifdef and # ifndef commands, which respectively indicate "if there is a definition" and "if there is no definition ".
# The general form of ifdef is:
# Ifdef macroname
Statement sequence
# Endif
If the macro name has been defined in the # def I n e Statement, the code block after the statement is compiled.
# The general form of ifndef is:
# Ifndef macroname
Statement sequence
# Endif
If the macro name is not defined in the # define statement, compile the code block.
# Ifdel and # ifndef can be used in # else statements, but # elif cannot. See 4-1 5.


(400) {this. resized = true; this. width = 400; this. alt = 'click here to open new window';} "onmouseover =" if (this. resized) this. style. cursor = 'hand'; "onclick =" window. open ('HTTP: // www.pcvz.com/Program/image/200602/2006210162547987.GIF'); ">

The above code prints "Hi Ted" and "RALPH not defined ". If t e d is not defined, "H I a n y o n e" is displayed, followed by "RALPH not defined ".
You can nest # ifdef and # ifndef to any depth like nested # if.
4.7.6 # undef
Command # undef cancel the macro name defined previously. The general format is:
# Undef macroname
For example:
# Deprecision LEN 100
# Difine WIDTH 100
Char array [LEN] [WIDTH];
# Undef LEN
# Undef WIDTH
/* At this point both LEN and WIDTH are undefined */
Until the # undef statement is run, both l e n and w I D T H are defined.
# The main purpose of undef is to limit the macro names to the code segment that only needs them.
4.7.7 # line
Command # line changes the content of _ LINE _ and _ f I L E _, which are pre-defined identifiers in the Compilation Program.
The basic command format is as follows:
# Line number ["filename"]
The number is a positive integer, and the optional file name is any valid file identifier. The row number is the current row number in the source program, and the file name is the name of the source file. Command # line is mainly used for debugging and other special applications.
For example, the row count starts from 1 0 0. The printf () statement shows 1 0 2 because it is the first row after the statement # line 100.
# Line 100/* initialize the row counter */
Main ()/* row number 100 */
{/* Row number 101 */
P r I n t f ("% d \ n", _ line _);/* row number 102 */
}
4.7.8 # pragma
Command # pragma is a real-time defined command that allows various commands to be sent to the compiler. For example, a compilation program may have an option that supports tracking program execution. Use the # p r a g m a statement to specify a trace selection.
4.7.9 predefined macro name
The a n s I Standard specifies five predefined macro names. They are:
_ Line _
_ F I L E _
_ D a t e _
_ T I M E _
_ S t d c _
If compilation is not standard, only a few of the above macro names are supported or not supported at all. Remember that the Compilation Program may also provide other predefined macro names.
The _ line _ and _ f I L E _ macro commands have been discussed in the # line Section. The remaining macro names are discussed here.
The _ d at e _ macro command contains a string in the form of month, day, or year, which indicates the date when the source file was translated to the code.
The time when the source code is translated to the target code is included in _ t I M E _ as a string. String format: minute: second.
If the implementation is standard, the macro _ s t d c _ contains the decimal constant 1. If it contains any other number, the implementation is non-standard.
Note: The macro name is written by an identifier and two underscores (_) on both sides.
4.7.10 notes
In C, all comments start with the character * and end. Spaces are not allowed between asterisks and slashes. The compiler ignores any text between the annotation start character and the annotation end character. For example, the following program only prints on the screen
"H e l o ".
Main ()
{
P r I n t f ("hello ");
/* Printf ("This is a sample to print hell; o "*/)
}
Annotations can appear anywhere in the program, but they cannot appear in the middle of a keyword or identifier.
That is, the annotation x = 10 +/* add the numbers */5; is valid, but swi/* this will not work */tch (c ){...
Is incorrect, because the C keyword cannot contain comments. Usually, you do not want comments in the middle of the expression, because this causes
Meaning ambiguous.
Annotations cannot be nested, that is, one annotation cannot contain another annotation. For example, the following code segment has an error during compilation:
/* This is an outer comment
X = y/;
/* This is an inner comment-and causes an error */
*/
Annotations should be concise when you need to explain program behavior. In addition to the simplest and most intuitive functions, there should be comments to describe the functions, how to call them, and where to return them at the beginning of the function.
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.