C ++ pre-processing command

Source: Internet
Author: User

The pre-processing functions provided by C ++ include macro definition, file inclusion, and Conditional compilation. They are implemented by macro definition commands, file inclusion commands, and Conditional compilation commands.

Preprocessing command

Format: # preprocessing command ...... (No credit points at the end)

Scope: from the definition pointProgramEnd

Description: The pre-processing command occupies only one row and has any location.

Macro definition command

Format: # define macro name [(parameter)] macro

# UNDEF macro name [(parameter)]

Scope: from the definition point to # UNDEF; otherwise, the entire file ends.

Description: The macro name can be an identifier without parameters or an identifier with parameters;

A macro can be any character sequence, but also an expression or multiple statements;

The keyword UNDEF is used to cancel the definition of the identifier. The canceled identifier can be used as another identifier;

The parameter is not a variable. It is just a symbol and has no type identifier;

Macro expansion is just a mechanical replacement.

Example:

# Define PI 3.1415

# Define s (r) pI * r // defines the macro S (R), and calculates the area of the circle whose radius is R.

File Inclusion command

Format: # include <File Name> or # include "file name"

Note: Angle brackets indicate that the included files are retrieved based on the system standard library path;

Double quotation marks indicate that the file to be included is first searched for in the file directory where the source file to be referenced is included. If the file cannot be found, the file to be included will be searched according to the standard library path of the system.

Include, or search for included files by path specified in double quotation marks. If no path is specified, search for included files in the current directory;

There are two types of files: header file (. h) and source file (. cpp );

Use <> for system files and "" for user-defined files "".

Conditional compilation command

Format: # If ...... # Endif

# If ...... # Else ...... # Endif

# If ...... # Elif ...... # Elif ...... # Else ...... # Endif

# Ifdef ...... # Else ...... # Endif

# Ifndef ...... # Else ...... # Endif

Note: Using Conditional compilation can avoid repeatedly containing a header file in a program.

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.