C preprocessing command

Source: Internet
Author: User

Address: http://www.51cto.com/html/2005/0927/3844.htm

I. Origin of preprocessing:
In the historical development of C ++, many language features (especially the obscure language) come from the C language. preprocessing is one of them. C ++ inherits the C language Preprocessor from the C language (C language Preprocessor, called Cpp by Dr. Bjarne, is short for C Program Preprocessor ).

Ii. Common preprocessing functions:
The main function of the pre-processor is to replace a resource with the built-in pre-processing functions. The most common pre-processing methods include file inclusion, Conditional compilation, layout control, and macro replacement.
File Inclusion: # include is the most common preprocessing, mainly used as the reference and combination of source code body of the file.
Conditional compilation: # if, # ifndef, # ifdef, # endif, # undef and so on are also common preprocessing tasks. They are mainly selected during compilation, comment out some specified code to achieve version control and prevent repeated inclusion of files.
Layout control: # progma, which is also an important aspect of application preprocessing. The main function is to provide unconventional Control Flow Information for compilation programs.
Macro replacement: # define, which is the most common usage. It can define symbol constants, function functions, renaming, String concatenation, and other functions.

Iii. Pre-processing commands:
The format of preprocessing commands is as follows:
# Directive tokens
# The symbol should be the first non-null character in this line. Generally, we place it at the starting position. If the command line cannot be placed, you can control it through/, for example:
# Define Error if (error) exit (1) is equivalent
# Define Error/
If (error) exit (1)
However, for the sake of beautification, we generally do not use this method. The more common method is as follows:
# Ifdef _ BORLANDC __
If_true <(is_convertible <Value, named_template_param_base >:: value)> ::
Template then <make_named_arg, make_key_value>: type Make;
# Else
Enum {is_named = is_named_parameter <Value >:: value };
Typedef typename if_true <(is_named) >:: template
Then <make_named_arg, make_key_value>: type Make;
# Endif
The following describes common preprocessing commands:
# Define macro definition
# Undef undetermined macros
# Include text inclusion
# Ifdef compile if the macro is defined
# Ifndef compile if the macro is not defined
# Endif end compilation block control
# Compile the Code if the expression is non-zero
# Compile else as the remaining options for other preprocessing
# Elif this is a combination of # else and # if
# Line: change the current number of lines and file name
# Error output an error message
# Pragma provides unconventional control flow information for the Compilation Program
Next we will describe these preprocessing items one by one. Considering the importance and complexity of macros, let's put them to the end.

Iv. File Inclusion commands:
This kind of preprocessing is the most common method, which is usually used in programming. The most common usage is:
# Include <iostream> // standard library header file
# Include <iostream. h> // old standard library header file
# Include "IO. h" // custom header file
# Include "../file. h" // header file under the parent directory of UNIX
# Include "/usr/local/file. h" // full path under UNIX
# Include "../file. h" // header file under the parent directory of Dos
# Include "/usr/local/file. h" // full path under Dos
Note the following two points:
1. Do we use <iostream> or <iostream. h>?
We advocate using <iostream> instead of <iostream. h>. Why? I think you may still remember the reasons I have given. Here I will give a rough explanation: first of all ,. the header file in the h format was abandoned by the Standards Committee as early as September. We should keep up with the standards to adapt to the development of the times. Second, iostream. h only supports narrow character sets, while iostream supports narrow/wide character sets.
In addition, the iostream standard has made many changes, and both interfaces and implementations have changed. Finally, all iostream components are put into namespace std to prevent name contamination.
2. What is the difference between <io. h> and "io. h?
In fact, their only difference is that the search path is different:
For # include <io. h>, the compiler starts searching from the standard library path.
For # include "io. h", the compiler searches
5. Compile control commands:
The main purpose of these commands is to select and comment out some specified code during compilation to achieve version control and prevent repeated inclusion of files.
Format:
1,
# Ifdef identifier
Your code
# Endif
If identifier is a defined symbol, the your code will be compiled; otherwise, it will be removed.
2,
# Ifndef identifier
Your code
# Endif
If identifier is an undefined symbol, the your code will be compiled; otherwise, it will be removed.
3,
# If expression
Your code
# Endif
If the expression is non-zero, the your code will be compiled; otherwise, it will be removed.
4,
# Ifdef identifier
Your code1
# Else
Your code2
# Endif
If identifier is a defined symbol, your code1 will be compiled; otherwise yourcode2 will be compiled
5,
# If expressin1
Your code1
# Elif expression2 // Haha, elif
Your code2
# Else
Your code3
# Enif
If epression1 is non-zero, your code1 is compiled. Otherwise, if expression2 is non-zero, your code2 is compiled. Otherwise, your code3 is compiled.

Other pre-compiled commands
In addition to the commonly used compilation commands in the above section, there are also three uncommon compilation commands: # line, # error, and # pragma. Let's talk about them briefly.
# Line Syntax:
# Line number filename
Example: # line 30 a. h where the file name a. h can be omitted without writing.
This command can change the current row number and file name. For example, the preceding preprocessing command can change the current row number to 30 and the file name to a. h. It seems to be useless at first, but it is still a bit useful, that is, it is used in the compilation of the compiler. We know that the compiler will generate some intermediate files during the compilation of C ++ source code, this command ensures that the file name is fixed and will not be replaced by these intermediate files, which is conducive to analysis.
# Error Syntax:
# Error info
Example: # ifndef UNIX
# Error This software requires the unix OS.
# Endif
This command mainly gives an error message. In the above example, if it is not in a UNIX environment, it will output This software requires the unix OS. Then it will induce the compiler to terminate. So in general, the purpose of this command is to give some information before the program crashes.
# Pragma is non-uniform and relies on various compiler producers. For example, in the sun c ++ Compiler:
// Adjust the start address of name and val to a multiple of 8 bytes.
# Progma align 8 (name, val)
Char name [9];
Double val;
// Call the MyFunction function at the beginning of Program Execution
# Progma init (MyFunction)
Predefined identifier
To process some useful information, preprocessing defines some preprocessing identifiers. Although the preprocessing identifiers of various compilers are different, they all process the following four types:
_ FILE _ name of the FILE being compiled
_ LINE _ the row number of the file being compiled
_ DATE string at the time of compilation, for example, "25 Dec 2000"
_ TIME string of the Compilation TIME, for example, "12:30:55"
Example: cout <"The file is:" <__file _ "<"! The lines is: "<__ LINE __< <endl;

Where to go for preprocessing
We will not discuss how to replace the # include pre-processing command here.
C ++ does not provide an alternative form for # include, but namespace provides a scope mechanism that supports combination in some way and can improve the behavior of # include, but we still cannot replace # include.
# Progma should be regarded as a dispensable pre-processing command. According to Bjarne, the father of C ++, it is: "# progma is often used to hide semantic deformation in the compilation system, or to provide language extensions with special semantics and clumsy syntax."
For # ifdef, we are still helpless, even if we use the if statement and constant expression, it is still not enough to replace her, because the body of an if statement must be syntactically correct to meet the class check, even if he is in a branch that will never be executed.

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.