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

Discussion in 'C Programming' started by Jimmy, Sep 9, 2011.

  1. Jimmy

    Jimmy Guest

    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
    Jimmy, Sep 9, 2011
    #1
    1. Advertising

  2. Jimmy

    Eric Sosman Guest

    Re: Is a blank at the end of a C file a better style than a newlineat the end of the file or is it better to place none?

    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 [...]

    --
    Eric Sosman
    d
    Eric Sosman, Sep 10, 2011
    #2
    1. Advertising

  3. Re: Is a blank at the end of a C file a better style than a newline at the end of the file or is it better to place none?

    Eric Sosman <> writes:
    > 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. 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).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 10, 2011
    #3
  4. Jimmy

    Eric Sosman Guest

    Re: Is a blank at the end of a C file a better style than a newlineat the end of the file or is it better to place none?

    On 9/10/2011 3:10 AM, Keith Thompson wrote:
    > Eric Sosman<> writes:
    >> 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.

    --
    Eric Sosman
    d
    Eric Sosman, Sep 10, 2011
    #4
  5. Re: Is a blank at the end of a C file a better style than a newline at the end of the file or is it better to place none?

    Eric Sosman wrote:

    > 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 ... ;
    --
    Roberto Waltman

    [ Please reply to the group,
    return address is invalid ]
    Roberto Waltman, Sep 10, 2011
    #5
  6. Jimmy

    ec429 Guest

    Re: Is a blank at the end of a C file a better style than a newlineat the end of the file or is it better to place none?

    On 10/09/11 16:36, Roberto Waltman wrote:
    > Eric Sosman wrote:
    >> 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 ... ;

    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
    --
    'sane', adj.: see 'unimaginative'
    on the web - http://jttlov.no-ip.org
    ec429, Sep 10, 2011
    #6
  7. Re: Is a blank at the end of a C file a better style than a newline at the end of the file or is it better to place none?

    In article <>,
    Roberto Waltman <> wrote:
    >Eric Sosman wrote:
    >
    >> 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 ... ;


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

    --
    "Remember when teachers, public employees, Planned Parenthood, NPR and PBS
    crashed the stock market, wiped out half of our 401Ks, took trillions in
    TARP money, spilled oil in the Gulf of Mexico, gave themselves billions in
    bonuses, and paid no taxes? Yeah, me neither."
    Kenny McCormack, Sep 10, 2011
    #7
  8. Jimmy

    Nomen Nescio Guest

    Re: Is a blank at the end of a C file a better style than a newline at none?

    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!
    Nomen Nescio, Sep 10, 2011
    #8
  9. Re: Is a blank at the end of a C file a better style than a newline at none?

    Nomen Nescio <> writes:

    > 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
    Patrick Scheible, Sep 11, 2011
    #9
  10. Re: Is a blank at the end of a C file a better style than a newline at none?

    In article <>,
    Patrick Scheible <> wrote:
    >Nomen Nescio <> writes:
    >
    >> 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


    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.
    Kenny McCormack, Sep 11, 2011
    #10
  11. Re: Is a blank at the end of a C file a better style than a newline at the end of the file or is it better to place none?

    Eric Sosman <> writes:
    > On 9/10/2011 3:10 AM, Keith Thompson wrote:
    >> Eric Sosman<> writes:
    >>> 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".

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 11, 2011
    #11
  12. Jimmy

    Jimmy Guest

    Re: Is a blank at the end of a C file a better style than a newlineatnone?

    On Sun, 11 Sep 2011 12:45:38 +0000, Kenny McCormack wrote:
    > In article <>, Patrick Scheible
    > <> wrote:
    >>Nomen Nescio <> writes:
    >>
    >>> 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

    >
    > 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
    Jimmy, Sep 11, 2011
    #12
  13. Re: Is a blank at the end of a C file a better style than a newlineatnone?

    On Sat, 10 Sep 2011 20:46:51 -0700, Patrick Scheible wrote:

    > Nomen Nescio <> writes:
    >
    >> 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


    Two.
    Kelsey Bjarnason, Sep 20, 2011
    #13
  14. Re: Is a blank at the end of a C file a better style than a newlineat none?

    Jimmy <> wrote:
    > ... 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.

    --
    Peter
    Peter Nilsson, Sep 21, 2011
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Bencsik
    Replies:
    2
    Views:
    808
  2. js
    Replies:
    2
    Views:
    384
  3. ÖܼÃÊÇĸÀÏÊó
    Replies:
    3
    Views:
    1,782
    Benjamin
    Oct 29, 2008
  4. Jimmy
    Replies:
    3
    Views:
    310
    J. J. Farrell
    Sep 9, 2011
  5. Cristina
    Replies:
    2
    Views:
    119
    Nate St.Germain
    Jul 16, 2010
Loading...

Share This Page