<p>What is the reason for the following warning in some C++ compilers?</p>

<blockquote>
  <p>No newline at end of file</p>
</blockquote>

<p>Why should I have an empty line at the end of a source/header file?</p>

[{'answer_id': 72297, 'author': 'Leigh Caldwell', 'author_id': 3267, 'author_profile': 'https://Stackoverflow.com/users/3267', 'pm_score': 3, 'selected': False, 'text': "<p>It isn't referring to a blank line, it's whether the last line (which can have content in it) is terminated with a newline.</p>\n\n<p>Most text editors will put a newline at the end of the last line of a file, so if the last line doesn't have one, there is a risk that the file has been truncated. However, there are valid reasons why you might not want the newline so it is only a warning, not an error.</p>\n"}, {'answer_id': 72377, 'author': 'Vytautas Šaltenis', 'author_id': 6763, 'author_profile': 'https://Stackoverflow.com/users/6763', 'pm_score': 4, 'selected': False, 'text': '<p>The answer for the "obedient" is "because the C++03 Standard says the behavior of a program not ending in newline is undefined" (paraphrased).</p>\n\n<p>The answer for the curious is here: <a href="http://gcc.gnu.org/ml/gcc/2001-07/msg01120.html" rel="nofollow noreferrer">http://gcc.gnu.org/ml/gcc/2001-07/msg01120.html</a>.</p>\n'}, {'answer_id': 72391, 'author': 'moonshadow', 'author_id': 11834, 'author_profile': 'https://Stackoverflow.com/users/11834', 'pm_score': 3, 'selected': False, 'text': '<p><code>#include</code> will replace its line with the literal contents of the file. If the file does not end with a newline, the line containing the <code>#include</code> that pulled it in will merge with the next line.</p>\n'}, {'answer_id': 72409, 'author': 'TJ Seabrooks', 'author_id': 3022, 'author_profile': 'https://Stackoverflow.com/users/3022', 'pm_score': 9, 'selected': True, 'text': "<p>Think of some of the problems that can occur if there is no newline. According to the ANSI standard the <code>#include</code> of a file at the beginning inserts the file exactly as it is to the front of the file and does not insert the new line after the <code>#include &lt;foo.h&gt;</code> after the contents of the file. So if you include a file with no newline at the end to the parser it will be viewed as if the last line of <code>foo.h</code> is on the same line as the first line of <code>foo.cpp</code>. What if the last line of foo.h was a comment without a new line? Now the first line of <code>foo.cpp</code> is commented out. These are just a couple of examples of the types of problems that can creep up.</p>\n\n<hr>\n\n<p>Just wanted to point any interested parties to James' answer below. While the above answer is still correct for C, the new C++ standard (C++11) has been changed so that this warning should no longer be issued if using C++ and a compiler conforming to C++11.</p>\n\n<p>From C++11 standard via James' post: </p>\n\n<blockquote>\n  <p>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 (C++11 §2.2/1).</p>\n</blockquote>\n"}, {'answer_id': 72450, 'author': 'Igor Semenov', 'author_id': 11401, 'author_profile': 'https://Stackoverflow.com/users/11401', 'pm_score': 5, 'selected': False, 'text': '<p>C++03 Standard [2.1.1.2] declares:</p>\n\n<blockquote>\n  <p>... If a source file that is not empty does not end in a new-line character, or ends in a new-line character\n  immediately preceded by a backslash character before any such splicing takes place, the behavior is undefined.</p>\n</blockquote>\n'}, {'answer_id': 6773704, 'author': 'keya', 'author_id': 855566, 'author_profile': 'https://Stackoverflow.com/users/855566', 'pm_score': -1, 'selected': False, 'text': "<p>That's not an error. It's just a warning.</p>\n\n<p>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.</p>\n\n<p>Though, besides that, you should be using <code>#include &lt;iostream&gt;</code> instead of <code>&lt;iostream.h&gt;</code>. Then put in a <code>using std::cout;</code> after it.</p>\n"}, {'answer_id': 8172348, 'author': 'James McNellis', 'author_id': 151292, 'author_profile': 'https://Stackoverflow.com/users/151292', 'pm_score': 6, 'selected': False, 'text': '<p>The requirement that every source file end with a non-escaped newline was removed in C++11.  The specification now reads:</p>\n\n<blockquote>\n  <p>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 (C++11 §2.2/1).</p>\n</blockquote>\n\n<p>A conforming compiler should no longer issue this warning (at least not when compiling in C++11 mode, if the compiler has modes for different revisions of the language specification).</p>\n'}, {'answer_id': 8771096, 'author': 'mwfearnley', 'author_id': 446106, 'author_profile': 'https://Stackoverflow.com/users/446106', 'pm_score': 0, 'selected': False, 'text': "<p>This warning might also help to indicate that a file could have been truncated somehow.  It's true that the compiler will probably throw a compiler error anyway - especially if it's in the middle of a function - or perhaps a linker error, but these could be more cryptic, and aren't guaranteed to occur.</p>\n\n<p>Of course this warning also isn't guaranteed if the file is truncated immediately after a newline, but it could still catch some cases that other errors might miss, and gives a stronger hint to the problem.</p>\n"}, {'answer_id': 13817529, 'author': 'divesh', 'author_id': 1894299, 'author_profile': 'https://Stackoverflow.com/users/1894299', 'pm_score': 2, 'selected': False, 'text': "<p>I am using c-free IDE version 5.0,in my progrm either of 'c++' or 'c' language i was getting same problem.Just <strong>at the end of the program</strong> i.e. last line of the program(after braces of function it may be main or any function),<strong>press enter</strong>-line no. will be increased by 1.then execute the same program,it will run without error.</p>\n"}, {'answer_id': 27624582, 'author': 'Jan-Philip Loos', 'author_id': 605745, 'author_profile': 'https://Stackoverflow.com/users/605745', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>Of course in practice every compiler adds a new line after the #include. Thankfully. –  @mxcl</p>\n</blockquote>\n\n<p>not specific C/C++ but a C dialect: when using the <code>GL_ARB_shading_language_include</code> extension the glsl compiler on OS X warns you <strong>NOT</strong> about a missing  newline. So you can write a <code>MyHeader.h</code> file with a header guard which ends with <code>#endif // __MY_HEADER_H__</code> and you <strong>will</strong> lose the line after the <code>#include "MyHeader.h"</code> for sure.</p>\n'}, {'answer_id': 31850899, 'author': 'skyking', 'author_id': 4498329, 'author_profile': 'https://Stackoverflow.com/users/4498329', 'pm_score': 2, 'selected': False, 'text': '<p>Because the behavior differs between C/C++ versions if file does not end with new-line. Especially nasty is older C++-versions, fx in C++ 03 the standard says (translation phases):</p>\n\n<blockquote>\n  <p>If a source file that is not empty does not end in a new-line\n  character, or ends in a new-line character immediately preceded by a\n  backslash character, the behavior is undefined.</p>\n</blockquote>\n\n<p>Undefined behavior is bad: a standard conforming compiler could do more or less what it wants here (insert malicous code or whatever) - clearly a reason for warning.</p>\n\n<p>While the situation is better in C++11 it is a good idea to avoid situations where the behavior is undefined in earlier versions. The C++03 specification is worse than C99 which outright prohibits such files (behavior is then defined).</p>\n'}, {'answer_id': 73596800, 'author': 'Yasin Bekar', 'author_id': 12526239, 'author_profile': 'https://Stackoverflow.com/users/12526239', 'pm_score': 0, 'selected': False, 'text': '<p>In my case, I use KOTLIN Language and the compiler is on IntelliJ. Also, I am using a docker container with LINT to fix possible issues with typos, imports, code usage, etc. This error is coming from these lint fixes, most probably - I mean surely.</p>\n<p>In short, the error says, \'Add a new line at the end of the file\' That is it.</p>\n<p><a href="https://i.stack.imgur.com/Cnebk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Cnebk.png" alt="Added Line on the file at the END" /></a></p>\n<p>Before there was NO extra empty line:</p>\n<p><a href="https://i.stack.imgur.com/UlK8q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UlK8q.png" alt="Without new line on the file" /></a></p>\n'}]
['https://Stackoverflow.com/questions/72271', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']