Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Standards (newline ending of source files)

I am referring to: Why should text files end with a newline? One of the answers quotes the C89 standard. Which in brief dictates that a file must end with a new line, which is not immediately preceded by a backslash.

Does that apply to the most recent C++ standard?

#include <iostream>
using namespace std;

int main()
{
  cout << "Hello World!" << endl;
  return 0;
}
//\

Is the above valid? (Assuming there is a newline after //\, which I've been unable to display)

like image 805
Viktor Avatar asked Jul 22 '15 22:07

Viktor


People also ask

Should you end files with a newline?

So, it turns out that, according to POSIX, every text file (including Ruby and JavaScript source files) should end with a \n , or “newline” (not “a new line”) character. This acts as the eol , or the “end of line” character.

How do I fix warning no newline at end of file?

That's not an error. It's just a warning. Open the file in an editor, go to the last line of the file, and hit enter to add a blank line to the end of the file. Though, besides that, you should be using #include <iostream> instead of <iostream.

What is the newline character in C?

The newline character ( \n ) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen.

What does no newline at end of file mean?

It indicates that you do not have a newline (usually '\n' , aka CR or CRLF) at the end of file. That is, simply speaking, the last byte (or bytes if you're on Windows) in the file is not a newline.


1 Answers

The given code is legal in the case of C++, but not for C.

Indeed, the C (N1570) standard says:

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 for being part of such a splice. A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character before any such splicing takes place.

The C++ standard (N3797) formulates it a bit differently (emphasis mine):

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 for being part of such a splice. If, as a result, a character sequence that matches the syntax of a universal-character-name is produced, the behavior is undefined. A source file that is not empty and that does not end in a new-line character, or that ends in a new-line character immediately preceded by a backslash character before any such splicing takes place, shall be processed as if an additional new-line character were appended to the file.

like image 133
AlexD Avatar answered Oct 09 '22 13:10

AlexD