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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Think of some of the problems that can occur if there is no newline. According to the ANSI standard the
#includeof 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 offoo.his on the same line as the first line offoo.cpp. What if the last line of foo.h was a comment without a new line? Now the first line offoo.cppis 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: