Ошибка no newline at end of file

What is the reason for the following warning in some C++ compilers?

No newline at end of file

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

Brian Tompsett - 汤莱恩's user avatar

asked Sep 16, 2008 at 13:38

3

Think of some of the problems that can occur if there is no newline. According to the ANSI standard the #include 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 #include <foo.h> 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 foo.h is on the same line as the first line of foo.cpp. What if the last line of foo.h was a comment without a new line? Now the first line of foo.cpp is commented out. These are just a couple of examples of the types of problems that can creep up.


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.

From C++11 standard via James’ post:

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).

S.S. Anne's user avatar

S.S. Anne

15.1k8 gold badges38 silver badges75 bronze badges

answered Sep 16, 2008 at 13:49

TJ Seabrooks's user avatar

TJ SeabrooksTJ Seabrooks

20.1k6 gold badges32 silver badges30 bronze badges

5

The requirement that every source file end with a non-escaped newline was removed in C++11. The specification now reads:

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).

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).

answered Nov 17, 2011 at 18:30

James McNellis's user avatar

James McNellisJames McNellis

347k75 gold badges910 silver badges977 bronze badges

4

C++03 Standard [2.1.1.2] declares:

… If a source file that is not empty does not end in a new-line character, or ends in a new-line character
immediately preceded by a backslash character before any such splicing takes place, the behavior is undefined.

Lightness Races in Orbit's user avatar

answered Sep 16, 2008 at 13:52

Igor Semenov's user avatar

The answer for the «obedient» is «because the C++03 Standard says the behavior of a program not ending in newline is undefined» (paraphrased).

The answer for the curious is here: http://gcc.gnu.org/ml/gcc/2001-07/msg01120.html.

Lightness Races in Orbit's user avatar

answered Sep 16, 2008 at 13:46

Vytautas Šaltenis's user avatar

1

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.

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.

answered Sep 16, 2008 at 13:40

Leigh Caldwell's user avatar

Leigh CaldwellLeigh Caldwell

10.3k4 gold badges24 silver badges31 bronze badges

#include will replace its line with the literal contents of the file. If the file does not end with a newline, the line containing the #include that pulled it in will merge with the next line.

LogicStuff's user avatar

LogicStuff

19.4k6 gold badges54 silver badges74 bronze badges

answered Sep 16, 2008 at 13:47

moonshadow's user avatar

moonshadowmoonshadow

86.3k7 gold badges82 silver badges122 bronze badges

Of course in practice every compiler adds a new line after the #include. Thankfully. – @mxcl

not specific C/C++ but a C dialect: when using the GL_ARB_shading_language_include extension the glsl compiler on OS X warns you NOT about a missing newline. So you can write a MyHeader.h file with a header guard which ends with #endif // __MY_HEADER_H__ and you will lose the line after the #include "MyHeader.h" for sure.

answered Dec 23, 2014 at 16:47

Jan-Philip Loos's user avatar

I am using c-free IDE version 5.0,in my progrm either of ‘c++’ or ‘c’ language i was getting same problem.Just at the end of the program i.e. last line of the program(after braces of function it may be main or any function),press enter-line no. will be increased by 1.then execute the same program,it will run without error.

answered Dec 11, 2012 at 9:47

divesh's user avatar

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):

If a source file that is not empty does not end in a new-line
character, or ends in a new-line character immediately preceded by a
backslash character, the behavior is undefined.

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.

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).

answered Aug 6, 2015 at 8:43

skyking's user avatar

skykingskyking

13.7k1 gold badge35 silver badges56 bronze badges

1

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.

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.

answered Jan 7, 2012 at 16:19

mwfearnley's user avatar

mwfearnleymwfearnley

3,2232 gold badges32 silver badges35 bronze badges

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.

In short, the error says, ‘Add a new line at the end of the file’ That is it.

Added Line on the file at the END

Before there was NO extra empty line:

Without new line on the file

answered Sep 4, 2022 at 4:32

Yasin Bekar's user avatar

I got the same error below when using Flake8:

no newline at end of fileFlake8(W292)

Because I didn’t add one blank line after the last code print(math.pi) as shown below:

1 import math
2
3 print(math.pi)

So, I added one blank line after the last code print(math.pi) as shown below, then the error was solved:

1 import math
2
3 print(math.pi)
4

Actually, I saw PEP 8 but I could not find why to add one blank line after the last code but the error below also occurred in the same situation when using Pylint:

Final newline missingPylint(C0304:missing-final-newline)

So, I think that adding one blank line after the last code is recommanded in Python even though I don’t know the reason.

In addition, if you add more than one blank lines after the last code print(math.pi) as shown below:

1 import math
2
3 print(math.pi)
4
5

Then, you get the errors below with Flake8 and Pylint respectively:

blank line at end of fileFlake8(W391)

Trailing newlinesPylint(C0305:trailing-newlines)

Files should end with a newline.

Anti-pattern

Imagine the example below is an entire file.

import os

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

Best practice

Imagine the example below is an entire file.

import os

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# This is a new line that ends the file.

No newline at end of file

Files should end with a newline.

Anti-pattern

Imagine the example below is an entire file.

import os

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

Best practice

Imagine the example below is an entire file.

import os

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# This is a new line that ends the file.


Go to Hyperskill


Warning PEP 8: W292 no newline at end of file in PyCharm

I have been using Intellij for a while but I just downloaded PyCharm to start working on the Python side of JetBrains Academy. So far, the first exercises that I have done with print() are always highlighting a warning in the closing parenthesis of print with the message PEP 8: W292 no newline at end of file and I don’t know what this means, did I do something wrong while installing or configuring PyCharm? If it is irrelevant, is there a way to stop from showing? Thanks!

Понравилась статья? Поделить с друзьями:
  • Ошибка no more mirrors to try
  • Ошибка no matching distribution found for
  • Ошибка no module named pandas
  • Ошибка no match for operator operand types are std
  • Ошибка no module named numpy