Why should text files end with a newline?

后端 未结 18 1265
栀梦
栀梦 2020-11-21 22:56

I assume everyone here is familiar with the adage that all text files should end with a newline. I\'ve known of this \"rule\" for years but I\'ve always wondered — why?

18条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 23:31

    Each line should be terminated in a newline character, including the last one. Some programs have problems processing the last line of a file if it isn't newline terminated.

    GCC warns about it not because it can't process the file, but because it has to as part of the standard.

    The C language standard says A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character.

    Since this is a "shall" clause, we must emit a diagnostic message for a violation of this rule.

    This is in section 2.1.1.2 of the ANSI C 1989 standard. Section 5.1.1.2 of the ISO C 1999 standard (and probably also the ISO C 1990 standard).

    Reference: The GCC/GNU mail archive.

提交回复
热议问题