End of line (GNU documentation)

I am reading gcc preprocessing documentation, I read the following sentence ( here ):

If there is no end of line marker in the last line of any input file, the end of the file is considered an implicit sentence. The C standard states that this condition causes undefined behavior, so GCC will issue a warning message.

I am trying to make a warning by doing:

> echo -n "int main(void) {return 0;}" > test.c > gcc -Wall -Wextra -Werror test.c 

But no problem, it compiles. I understand the end-of-line marker as a new-line char, but there seems to be something else.

How can I create a warning?

+6
source share
2 answers

It seems to have retired from gcc.

See this: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14331#c19

2007-05-31

  PR preprocessor/14331 * lex.c (_cpp_get_fresh_line): Don't warn if no newline at EOF. 
+5
source

C11 (N1570), Β§5.1.1.2,:

A source file that is not empty must end with a newline, which will not immediately be preceded by a backslash before such a merge occurs.

Thus, not having a new line in the file violates the above β€œmust” restriction.

clang warns about this:

 $ printf "int main(void) {return 0;}" > test.c $ clang -Weverything test.c test.c:1:27: warning: no newline at end of file [-Wnewline-eof] int main(void) {return 0;} ^ 1 warning generated. 

But breaking the restriction is undefined behavior .

C11 (N1570), Β§4,:

If "or" will not require it to appear outside of the time limit or run time limit, the behavior is undefined. Undefined behavior is indicated differently by the International Standard with the words "undefined or the absence of an explicit definition of behavior. There is no difference in emphasis among the three; they all describe undefined behavior.

Thus, gcc took the liberty of issuing warnings (as @nnn indicated), since gcc does not need to issue any diagnostics for Undefined behavior.

+1
source

All Articles