warning no newline at the end of file
main.c :10:2 warning: no newline at the end of file
修復這個警告,在文件結尾回車一下就行了。可以很少會有人去仔細探究,為什么gcc會給出這么一個警告?
原因其實也很簡單,因為標準規定如此。C99第5.1.1.2節這樣寫道:
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 lineshall 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.
C99 Rationale中進一步指出:
A backslash immediately before a newline has long been used to continue string literals, as well as preprocessing command lines. In the interest of easing machine generation of C, and of transporting code to machines with restrictive physical line lengths, the C89 Committee generalized this mechanism to permit any token to be continued by interposing a backslash/newline sequence.
這么規定的初衷有兩個:
為了每一行都要以換行結束。
因為行尾的/表示連接下一行,如果一個文件最后一行(main函數的右邊大括號)行尾有/,那么,緊跟它也被包含進來的下一個源文件的第一行就會被連接!而如果一個文件以一個空行結束就會避免這種情況的發生。
例子:
上例中的第四個物理行和第五個物理行被反斜杠(/)連接成一個邏輯行(等價于printf("%d", sizeof(int)));
如果在第七行main函數的右大括號后面加上反斜杠的話,那么它就會和下一個文件的第一行連接,所以這里}之后要輸入一個換行符,故要求每一行都要用換行結束。
注:有些編譯器會自動在文件結束位置插入換行符。(譬如vi編輯器)
總結
以上是生活随笔為你收集整理的warning no newline at the end of file的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 帽子戏法!《波斯王子:时之砂重制版》三宣
- 下一篇: sqlite3_exec