Functions and usage of C ++ & quot; # & quot;

Source: Internet
Author: User

Functions and usage of C ++ "#"
1#And##Functions and usage

In the macro of C/C ++,#The function is to stringize the macro parameters following it. Simply put, a double quotation mark is added to the left and right sides of the macro variables referenced by the macro variables.##A Concatenation symbol is composed of two well numbers. Its function is to concatenate two sub-numbers in the macro definition with parameters to form a new sub-string. But it cannot be the first or last substring.

#include 
  
   using namespace std;#define WARN_IF(EXP) if(EXP) cerr << #EXP << endl;#define paster( n ) cout << "token" << #n << " = " << n << endl;#define _CONS(a, b) int(a##+##b)#define _STRI(s) #svoid main(){    int div = 0;    WARN_IF(div == 0);           // prints : div == 0    paster(9);                   // prints : token9 = 9    cout << _CONS(1, 2) << endl;     // prints : 3    cout << _STRI(INT_MAX) << endl;  // prints : INT_MAX}
  

Useful in macro definition#Or##LocationMacro ParametersIs not expanded, such_STRI(INT_MAX)InINT_MAXIt will not be expanded to 2147483647. If you want to expand the macro parameters, you need to add an intermediate conversion macro:

#define STRI(s) _STRI(s)cout << STRI(INT_MAX) << endl; // prints : 2147483647

The purpose of adding this macro layer is to expand all macro parameters in this layer, so that the macro in the conversion macro can get the corresponding macro parameters.

Next, let's take a look at some usage of controlling code compilation by creating Conditional compilation parameters through preprocessing commands.

2#includeUsage

Operations that contain header files generally have two formats:

#include 
  
   #include "header-file"
  

<>And""Indicates that the sequence of the compiler to search for header files is different:

<>Indicates to start searching from the system directory, and then search for the directory listed by the PATH environment variable without searching the current directory. ""Is to start searching from the current directory, and then the directories listed by the system directory and PATH environment variables.

Therefore, system header files are generally used<>You can use""To accelerate the search speed. In addition, when writing more code, you will find that some header files have hidden dependencies between them, so be sure to pay attention to them. Google C ++ Style Guide also emphasizes that using standard header file inclusion sequence can enhance readability and avoid hiding dependencies:

1. Related Files (priority, as shown in figuredir2/foo2.h)
2 C System File
3 C ++ system files
4..hFile
5. Within the Project.hFile

3#if,#elif,#else,#endifUsage
// structure 1#if constant_expression#else#endif// structure 2#if constant_expression#elif constant_expression#endif

The structure and commonif...elseAndif...else if...elseStatement is similar, when#ifWhen the condition is non-zero (true ),#ifAnd#elseOr#elifOtherwise, compile#elseAnd#endifBetween code (or judgment#elifDetermines whether to compile if the condition is non-zero (true ).#elifAnd#endif).

#if 1    cout << "Hello world!" << endl;#else    cout << "Nice to meet you!" << endl;#endif// prints : Hello world!
#if 1    cout << "Hello world!" << endl;#elif 1    cout << "Nice to meet you!" << endl;#endif// prints: Hello world!//         Nice to meet you!

4#define,#undef,#ifdef,#ifndefUsage

#defineIs a common macro definition method. Its usage structure is as follows:

// #define identifier replacement-code#define PI 3.1415926#define ADD(x,y) (x + y)

#undefAs the name impliesFrom thereCancel the macro defined earlier. If the identifier is not defined as a macro name, this command is ignored:

// #undef identifier#undef PI

#ifdefAnd#ifndefOn the contrary, the former means that if the macro is defined, the corresponding code is compiled; the latter means that if the macro is not defined, the corresponding code is compiled. The general structure is:

/* * #ifdef identifier * #else or #elif * #endif**/ #define DEBUG#ifdef DEBUG  cout << "This is a debug message." << endl;#endif// prints : This is a debug message./* * #ifndef identifier * #else or #elif * #endif**/ #ifndef DEBUG  cout << "This is a debug message." << endl;#endif// prints nothing

During programming, to avoid redefinition of header files#defineSolved With Conditional compilation:

#ifndef MY_HEADER_FILE_H#define MY_HEADER_FILE_H// ...class MyHeaderFile{    // ....};#endif // MY_HEADER_FILE_H

In addition#pragma onceAs long as this command is added at the beginning of the header file, the header file can be compiled once. (In all preprocessing commands,#pragmaThe instruction may be the most complicated. Its function is to set the compiler status or instruct the compiler to complete some specific actions. This article will not detail much .)

5#lineUsage

#lineCommand is used to change__LINE__And__FILE__Variable value.__FILE__And__LINE__Description of the current file to be read and the number of rows.

// #line line-number filenameint main(){#line 10 "main.cpp"    cout << __FILE__ << " " << __LINE__ << endl;}// prints : main.cpp 10

6#errorUsage

#errorThis will directly cause the program to stop compilation and output the specified error message:

// #error message#ifndef VERSION#error Version number not specified.#endif// The compiler will halt compiling and return with the specified error message: // fatal error C1189: #error :  Version number not specified.

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.