Preprocessing (above) in C ++)

Source: Internet
Author: User

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> file: // standard library header file
# Include <iostream. h> file: // old standard library header file
# Include "Io. H" file: // custom header file
# Include "../file. H" file: // header file in the parent directory of UNIX
# Include "/usr/local/file. H" file: // complete path in UNIX
# Include "../file. H" file: // header file under the parent directory of DoS
# Include "/usr/local/file. H" file: // complete 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, the. h header file 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, your code2 will be compiled.
5,
# If expressin1
Your code1
# Elif expression2
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.

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.