Should a source file end with new line or not? What about backslash + newline?

If we don’t end a source file with a new line character but just a } after main(), then the clang compiler (in strict mode
-std=c17 -pedantic-errors) infamously complains:

error: no newline at end of file [-Werror,-Wnewline-eof]

So where did they get this from? It would seem taken from C17 5.1.1.2 Translation phases, 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. 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.

This first describes how the preprocessor merges lines in the presence of a . If would seem that the intention of this whole translation phase is to decribe line splicing specifically and not source files in general.

There seems to be some confusion about the last sentence. If I end my file with (new line) then now clang suddenly lets the code through in direct violation of the above quoted normative part in the standard. gcc however complains about the non-conformance:

error: backslash-newline at end of file

Sure, the translation phases are not part of a constraint so the compiler doesn’t have to generate a diagnostic. But why is clang behaving all backwards?

I checked the C99 rationale and it says that the intention of allowing backslash + new line as a general way to splice lines, string literals etc was added as per C89 for maximum portability: “the C89 Committee generalized this mechanism to permit any token to be continued by interposing a backslash/newline sequence”. This would be why backslash + new line with no trailing tokens after doesn’t make any sense.

9

The main question is if a C file should end with a new line or not and if so where in the C standard does it say so. The translation phase part was the only relevant part I could find.

Yes, that’s the part that says a source file must end with a (non-escaped) newline. You expressed doubt about the applicability of that requirement based on it being bundled with the specifications for line splicing, but I don’t see how that would matter. The spec requires a non-escaped newline at the end of the file, with the result that the source file overall, if non-empty, must have the form of a sequence of one or more newline-terminated lines (at the end of phase 2, but that requires the same at the beginning of phase 2).

If the spec merely wanted to forbid an escaped newline at the end of a source file then it would have said that, and the relevant language would have been simpler than what it actually does say. Something like this, for example: “Prior to such line splicing, a non-empty source file shall not end in a new-line character immediately preceded by a backslash character.”

I see no reason to interpret the requirement for a non-escaped newline at the end of a source file as meaning anything other than what it says on its face.

The bonus question is why clang is seemingly non-conforming when it comes to ending a source file with backslash + newline

Clang does not, for this reason, fail to conform. It accepts non-conforming source without emitting a diagnostic, but conforming implementations are permitted to do that as long as the non-conformance does not involve a constraint violation. I am inclined to guess that this particular omission is contrary to the intent of Clang’s authors, such that it constitutes a program flaw, but you would have to ask them. Possibly by submitting a bug report.

In any case, Clang’s behavior is not well characterized as “backwards”. It does diagnose one bona fide source-conformance issue, but fails to diagnose a different, related one. That may be flawed, but it doesn’t seem backwards to me.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật