Why is required to leave an empty line at the end of a C program?

I know it looks like a stupid question, but i really wanna know the reason.

Actually, i think it's because the c compiler will detect it as the end of file "EOF" of the program, but, am i wrong? because it compiles it anyway, but keep showing warnings like "no new line at the end of file".

I will appreciate any help, thank you.

The question isnt stupid but your compiler is...so change it.

Are you sure you mean an extra newline at the end of the file, as in, two? Or just one newline at the end of the file? One newline at the end of the file may look like an extra blank line but is really just one character. Many text editors won't even create a text file without at least one newline at the end of file.

Even gcc gives a warning about no newline at the end of the file(Not an error mind you, just a warning), it may be traditional/standard behavior. On one hand it's a stupid thing to be picky about, on the other hand it's a silly thing to quibble about either -- not hard to add one newline to one file once and be done with it.

Is that it? just standard behaviour? I thought there will be a logical reason for the new line at the end of the file.

And, yeah, i mean just one line..

Yes, it's part of the C standard. It's also so totally pointless and such a sore point for nitpickers that gcc 4.3 and newer dropped it.

Thank you man, your answers are always correct and successful. :b:

Not that pointless IMHO. Several traditional Unix utilities are expecting all lines to be completed and will silently ignore the last one if unterminated. Also concatenating such C source files might break both their syntax and on screen readability.

This problem was mostly academic during decades when all the editors were correctly finishing every line by a new-line character but happens now from time to time with either automatically or exotic editor generated code.

Interesting. Which utilities?

Both the SunOS 4 compatible and the default Solaris sed implementations silently ignore a last unterminated line. The posix compliant sed version doesn't ignore the last line but complain about it:

$ printf "abc\ndef\nghi" > file
$ /usr/ucb/sed '' file
abc
def
$ /usr/bin/sed '' file
abc
def
$ /usr/gnu/bin/sed '' file
abc
def
ghi$ /usr/xpg4/bin/sed '' file
abc
def
sed: Missing newline at end of file file.
ghi

Not also that both the Unix and Gnu versions of wc will tell that file has only two lines, which might confuse tools trusting that information:

  
$ wc -l file
       2 file
$ /usr/gnu/bin/wc -l file
2 file