Is a blank at the end of a C file a better style than a newline atthe end of the file or is it bette

J

Jimmy

Hello everyone!

As the subject says. The reason for my question is that emacs always
suggest me to put a newline at the end of the C file if I forget to do
it. Why?

Sincerely,
Jimmy
 
E

Eric Sosman

Hello everyone!

As the subject says. The reason for my question is that emacs always
suggest me to put a newline at the end of the C file if I forget to do
it. Why?

Because it's required. 5.1.1.2p2:

A source file that is not empty shall end in a new-line
character, which shall not be immediately preceded by a
backslash character [...]
 
K

Keith Thompson

Eric Sosman said:
Hello everyone!

As the subject says. The reason for my question is that emacs always
suggest me to put a newline at the end of the C file if I forget to do
it. Why?

Because it's required. 5.1.1.2p2:

A source file that is not empty shall end in a new-line
character, which shall not be immediately preceded by a
backslash character [...]

Yes, but that's not a constraint, which means that a compiler isn't
required to complain about it. It also means that the behavior of the
program is undefined.

In practice, one of the following will probably happen (in descending
order of likelihood, according to my vague guess):

1. The compiler warns about the missing newline but compiles the
file as if it were there.

2. The compiler doesn't warn about the missing newline but compiles
the file as if it were there.

3. The compiler prints an error message and rejects the source file.

4. The compiler compiles the source file without complaint as if
the last line were not there at all (leading to a possible syntax
error depending on what the last line looks like).

5. Something other arbitrarily bad thing happens.

To answer your question, a C source file should always end with
a newline. To answer your question about a blank rather than
a newline, I'm not entirely sure what you're asking. It's not
necessary to have either a space character or an empty line at the
end of a source file, just a newline character to terminate the last
line. For example, the last three characters of your file might be
'\n', '}', '\n' (on a system that uses '\n' to terminate lines).
 
E

Eric Sosman

Eric Sosman said:
Hello everyone!

As the subject says. The reason for my question is that emacs always
suggest me to put a newline at the end of the C file if I forget to do
it. Why?

Because it's required. 5.1.1.2p2:

A source file that is not empty shall end in a new-line
character, which shall not be immediately preceded by a
backslash character [...]

Yes, but that's not a constraint, which means that a compiler isn't
required to complain about it. [...]

Surely emacs is within its rights to recommend that a source
file be well-formed, even if the compiler is not required to diagnose
deviations?

Heck, emacs goes so far as to suggest line indentations, which
the compiler doesn't need at all.
 
R

Roberto Waltman

Eric said:
Surely emacs is within its rights to ...

Interesting. It seems not only corporations "are persons under the
law", but text editors were also granted personhood while I was not
looking ... ;
 
E

ec429

Interesting. It seems not only corporations "are persons under the
law", but text editors were also granted personhood while I was not
looking ... ;
Only emacs, and only since someone added self_aware.el to the library.
It was bound to happen eventually - LISP's always been a favourite
language of AI researchers...
-E
 
K

Kenny McCormack

Interesting. It seems not only corporations "are persons under the
law", but text editors were also granted personhood while I was not
looking ... ;

Don't anthropomorphize computers (or text editors). They hate that.
 
N

Nomen Nescio

Hey asshole, nobody wants to see your sorry ass leftist signature when you
quote a whole post just to follow up with a one-liner. Eat shit commie!
 
P

Patrick Scheible

Nomen Nescio said:
Hey asshole, nobody wants to see your sorry ass leftist signature when you
quote a whole post just to follow up with a one-liner. Eat shit commie!

Just for the record, at least one person thought it was an amusing
signature and enjoyed seeing it.

-- Patrick
 
K

Kenny McCormack

Just for the record, at least one person thought it was an amusing
signature and enjoyed seeing it.

-- Patrick

Thanks. In any case, I doubt anyone is going to give a second thought to
"Nomen"'s juvenile outburst.

--
Windows 95 n. (Win-doze): A 32 bit extension to a 16 bit user interface for
an 8 bit operating system based on a 4 bit architecture from a 2 bit company
that can't stand 1 bit of competition.

Modern day upgrade --> Windows XP Professional x64: Windows is now a 64 bit
tweak of a 32 bit extension to a 16 bit user interface for an 8 bit
operating system based on a 4 bit architecture from a 2 bit company that
can't stand 1 bit of competition.
 
K

Keith Thompson

Eric Sosman said:
Eric Sosman said:
On 9/9/2011 4:46 PM, Jimmy wrote:
Hello everyone!

As the subject says. The reason for my question is that emacs always
suggest me to put a newline at the end of the C file if I forget to do
it. Why?

Because it's required. 5.1.1.2p2:

A source file that is not empty shall end in a new-line
character, which shall not be immediately preceded by a
backslash character [...]

Yes, but that's not a constraint, which means that a compiler isn't
required to complain about it. [...]

Surely emacs is within its rights to recommend that a source
file be well-formed, even if the compiler is not required to diagnose
deviations?

Heck, emacs goes so far as to suggest line indentations, which
the compiler doesn't need at all.

Of course. vim (at least by default) doesn't even allow you to create a
file without a trailing newline.

But emacs isn't asking about the newline because C needs it; it does
that for all text files.

I was merely quibbling about the word "required".
 
J

Jimmy

Thanks. In any case, I doubt anyone is going to give a second thought
to "Nomen"'s juvenile outburst.

I found the sig funny and insightful too. Pretty depressing that the
lunatic GOP fringe spout their incoherent bullshit even on a day like
today. Their ludicrous dick-waving in Iraq successfully destroyed every
ounce of sympathy the rest of the world had for our country in the wake
of the 9/11 attacks. Bush and his cronies made the world a more dangerous
place: and America a less safe country. Nice work, ass hats.

Anyway, thanks for all the responses - guess I'll just accept emacs'
offer to add an extra newline at the end of my files.

Sincerely,
Jimmy
 
P

Peter Nilsson

Jimmy said:
... guess I'll just accept emacs' offer to add an extra
newline at the end of my files.

It's not an 'extra' newline. It's the terminating newline
for the last line in the file. If it added an empty line
at the end of your files, *then* it would be an extra
newline.

But where emacs and C are coming from is that some systems
actually require a trailing newline. A good example is old
line printers that wouldn't print the last line until it
was given a newline character that prompted it to dump it's
line buffer.

Perhaps more practically, consider the header files in a
program that begins...

#include <stdio.h>
#include <stdlib.h>

If the header <stdio.h> didn't end with a newline then
the first line of <stdlib.h> may be merged with the last
line of <stdio.h> producing, say...

#endif#ifndef __stdlib

....which would likely result in a compilation error.

Certainly this is an issue if you're dumping a lot of
text files (e.g. with more, less or TYPE.) Adding the
newline tends to be *much* less of an inconvenience than
not adding it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top