The backslash in C ++ acts as "\"

Source: Internet
Author: User
Today, I checked some of the content in the header file provided by VC. Many statements are followed by backslashes. In my impression, only // indicates the annotation function, however, the backslash is only an escape character. But I have never seen a backslash after the statement! After checking, it turns out to be like this:

① Escape characters; for example, \ n indicates carriage return + line feed.

② Continue the line operator. Note that in general statements, this has nothing to do with it, during compilation, the compiler automatically connects the two sentences (note that the first character (including space) of the original line of the second line is connected to the previous line). See the following example:

int i = 1; cout<<i++ <<i++<<i++<<endl;

This is correct.

However:

int i = 1; cout< <i++<<i++<<i++<<endl;

This will cause an error.

Then:

int i = 1;   cout<\   <i++<<i++<<i++<<endl;

But this is also a problem.

Then:

int i = 1; cout<\ <i++<<i++<<i++<<endl;

There is no problem!

Here, I see it!

,

In addition, you need to pay attention to the fact that the above line breaks are used to resume a row when a statement is relatively long. Another function is to use the backslash when the macro is defined as a long statement or a statement block or even a function! See the following:

Refer to Baidu knowledge
There is a formal name for this item called the line feed, which is the same as adding a don't add to the back of a common code line (VC automatically judges the line feed), but it is particularly useful in macro definition, because the macro definition must be completed in one line: # define somefun (X, a, B) if (x) x = a + B; else x = A-B; this line of definition is fine, but this code is not easy to understand and will be difficult to maintain in the future. If it is written as: # define somefun (X, a, B) if (X) X = a + B; else x = A-B; this is a good understanding, but the compiler will make an error because it will think that # define somefun (X, a, B) is a complete line. If (X) and the subsequent statements are irrelevant to # define somefun (X, a, B. in this case, we must use the following statement: # define somefun (X, a, B) \ If (x) \ x = a + B; \ else \ x = A-B; note: Do not add a line break to the last line. the pre-processor of VC automatically removes the \ and line feed carriage return before compilation, so that reading is not affected, but the logic is not affected.

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.