Issue 1098: Source files not ending with newline

Authors: Joseph Myers
Date: 2026-09-01
Submitted against: C2Y (N3886 draft)
Status: Review

As noted in reflector message 33995, when we accepted N3411, allowing source files without a final newline, we did not make any corresponding change to the syntax for many preprocessing directives, which includes new-line, as does that for text-line, so leaving syntax violations for source files without the final newline character, when the intent was to allow such file as if the final newline were added. It was also said when we accepted N3478, making partial comments at the end of a file into a constraint violation, that we should allow a // comment on the final line of a file without needing trailing newline.

In reflector message 34017, Corentin Jabot suggested following the wording used by C++. This would allow a source file with no final newline without allowing the case where the source file ends with a backslash (and no following newline) to act as if line splicing occurs. It would both resolve the issues introduced by the integration of N3411, and allow a // comment on the final line of a file without a trailing newline to be handled as such rather than being a constraint violation.

Suggested correction

At the end of translation phase 2 in 5.2.1.2 (Translation phases), add:

  1. 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 and that (after splicing) does not end in a new-line character is processed as if an additional new-line character were appended to the file.

Comment from Issues list maintainer on 2026-10-15:

Alejandro Colomar has expressed an intent to propose a constraint against source files without a final newline; this has been allocated document number N3988.


Comment from Issues list maintainer on 2026-11-25:

At the November 2026 (Búzios) meeting of WG14, the suggested correction was accepted (notwithstanding Alejandro Colomar's argument that any such change should be made to translation phase 1 rather than translation phase 2), subject to review at the April 2027 meeting.

Proposed correction

In the N3998 draft of C2Y, at the end of translation phase 2 in 5.2.1.2 (Translation phases), add:

  1. 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 and that (after splicing) does not end in a new-line character is processed as if an additional new-line character were appended to the file.

Comment from Alejandro Colomar on 2026-11-25:

Phase 1 specifies that characters are mapped in an implementation-defined manner, including new-line characters for EOL indicators. This is meant (among other things) to uniformize the line endings before further translation.

For example, a file consisting of

SPACE CR

might be translated to

SPACE LF

in some implementations (those that recognize CR as a valid EOL indicator).

This phase also supports translate of files without an EOL in the last line, into something that has a newline:

source:
	SPACE
phase 1:
	SPACE LF

Recently, we had a change (n3411) which changed phase 2 with the intent of removing UB, but that didn't take into consideration how that UB could be triggered. At least one of the following conditions was necessary (and sufficient) to trigger that UB in C23:

Let's use some practical cases.

These weird observable differences are a consequence of the wrong choice of place for a fix. The intended change is to require implementations to consider the end of a file to mean the end of the last line, and that should be specified in phase 1. Here's some text that would do it (based on n3998):

5.2.1.2 Translation phases (p1, bullet 1)

Physical source file multibyte characters are mapped, in an implementation-defined manner, to the source character set (introducing new-line characters for end-of-line indicators) if necessary. If a source file that is not empty does not end in an end-of-line indicator, the end of the source file is considered to indicate the end of the last line.

6.4.10 General (Constraints, p0+1)

A source file that is not empty shall end in a new-line character after splicing takes place in 5.2.1.2.

The practical cases above would be portable after such a change:

Those constraint violations are much preferable than the divergent behavior enabled by n3411 and Issue 1098.

This approach still forces POSIX (and only POSIX) to behave in a way that it doesn't seem to want to, which we shouldn't do, but at least it's better than the wrong approaches of n3411 and Issue 1098.