Thinking of reading "A thought caused by a detail"

Source: Internet
Author: User
Today, I happened to see this "a detail caused by thinking", not only relieved my usual confusion, but also caused me a little thought.

Know it but still need to know why, I think as CS students, this is the basic attitude should have.

First take a look at the GNU website's explanation for the problem ():


The C language standard says
A source file, is isn't empty shall end in a new-line character, which shall the not to immediately preceded by a backslash Character.

Since this are a "shall" clause, we must emit a diagnostic message for a violation the this rule.

This is in the section 2.1.1.2 of the ANSI C 1989 standard. Section 5.1.1.2 of the ISO C 1999 standard (and probably also the ISO C 1990 standard). "

So why is the standard so deliberately prescribed, there must be a suitable reason for it. With such a mentality, I turned to the standard corresponding paragraph

"5.1.1.2 translation Phases
[...]
2. Each instance of a backslash character (/) immediately
followed by a new-line character is deleted, splicing
Physical source lines to form logical source lines.
Only the last backslash on any physical source Line
shall be eligible to being part of such a splice. A
Source file, is not empty shall end in a new-line
character, which shall is immediately preceded by
A backslash character before any such splicing takes
Place.
"

I see. My understanding is that the standard is based on a common writing habit in C code that is too long to
A string that is contained within a line, usually written in the following form:

Char str[] = "It ' s a lonely lonely/
Night. " ;
This way, in accordance with the standard, the compiler reads the source code, read the first line above the '/' and immediately followed by the newline, know that the physical lines do not constitute a completed logical line, so will continue to read the next row of source code, the two lines of code splicing together, Compose a semantic complete logical line.

To maintain consistency, the standard stipulates that each physical line of the source code must end with a newline, and that for Non-null source code, the newline must be the Terminator, and the newline cannot be an escape character '/' before. Because this means there will be stitching behavior but the source file has no more physical line.

Alternatively, you can change the angle, assuming that a. h file is missing newline as the Terminator, then another source file #include the header file, there will be unexpected "stitching".

By the way, most editors (at least my vim in my hands) will automatically add newline at the end of the line, so there's usually no "warning:no newline at" file. "Warning:"


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.