info about EOF

Discussion in 'C Programming' started by alok, Mar 21, 2011.

  1. alok

    alok Guest

    if(getchar() != EOF)

    what is the value of EOF and for what keyborad input the if statement
    will fail?
     
    alok, Mar 21, 2011
    #1
    1. Advertising

  2. alok

    Seebs Guest

    On 2011-03-21, alok <> wrote:
    > if(getchar() != EOF)
    >
    > what is the value of EOF and for what keyborad input the if statement
    > will fail?


    This question seems awfully homeworky.

    So, help us out here. What have you already tried to do in order to
    figure out the answer to this question? What are you stuck on?

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Mar 21, 2011
    #2
    1. Advertising

  3. On Mon, 21 Mar 2011 14:19:58 -0400, alok <> wrote:

    > if(getchar() != EOF)
    >
    > what is the value of EOF and for what keyborad input the if statement
    > will fail?


    The value of EOF is EOF, which is a negative value of type (int), which
    cannot be represented in an (unsigned char). The exact value is up to
    the implementation, and you should try not to know what it is, because
    if you know the value you're liable to write code which expects that value,
    and that code will fail when you try to run it on a different platform.

    What keyboard input generates an EOF is system-dependent, and on the
    systems I most often write C code for, the EOF character can be changed
    by the user.
    --
    Morris Keesan --
     
    Morris Keesan, Mar 21, 2011
    #3
  4. On Mar 22, 1:03 pm, pete <> wrote:
    > Morris Keesan wrote:
    > > What keyboard input generates an EOF is system-dependent,
    > > and on the systems I most often write C code for,
    > > the EOF character can be changed by the user.

    >
    > What's an "EOF character" ?
    >

    In some systems, notably MS-DOS, end of file was indicated by a
    special character, control-Z.

    Since in C control-Z has to be treated as a valid char, fgetc() and
    related functions had to intercept control-Z and translate to an EOF.
    EOF is always negative.
     
    Malcolm McLean, Mar 22, 2011
    #4
  5. In article <op.vsplubdm5qv6o3@toshiba-laptop>,
    Morris Keesan <> wrote:
    >On Mon, 21 Mar 2011 14:19:58 -0400, alok <> wrote:
    >
    >> if(getchar() != EOF)
    >>
    >> what is the value of EOF and for what keyborad input the if statement
    >> will fail?

    >
    >The value of EOF is EOF, which is a negative value of type (int), which
    >cannot be represented in an (unsigned char). The exact value is up to
    >the implementation, and you should try not to know what it is, because
    >if you know the value you're liable to write code which expects that value,
    >and that code will fail when you try to run it on a different platform.


    Be wary anytime anyone tells you that you shouldn't know something. This is
    very much the stance of the medieval Church.

    If you want to know what EOF is on your system, try this:

    $ printf '#include <stdio.h>\nmain() {printf("EOF = %%d\\n",EOF);return 0;}'|gcc -xc - && ./a.out && rm ./a.out

    which displays EOF = -1 on my system.

    --
    Just for a change of pace, this sig is *not* an obscure reference to
    comp.lang.c...
     
    Kenny McCormack, Mar 22, 2011
    #5
  6. alok

    Mark Bluemel Guest

    On 03/22/2011 11:03 AM, pete wrote:
    > Morris Keesan wrote:
    >
    >> What keyboard input generates an EOF is system-dependent,
    >> and on the systems I most often write C code for,
    >> the EOF character can be changed by the user.

    >
    > What's an "EOF character" ?
    >

    I took it as being shorthand for
    "the keystroke (combination) which when entered on a keyboard
    (or similar input device) indicates to the operating system
    that end of file has been reached. This will be represented
    by the C support libraries by delivering the EOF value as the
    result of a read from this device."
     
    Mark Bluemel, Mar 22, 2011
    #6
  7. alok

    Mark Bluemel Guest

    On 03/22/2011 12:37 PM, Kenny McCormack wrote:
    > In article<op.vsplubdm5qv6o3@toshiba-laptop>,
    > Morris Keesan<> wrote:
    >> On Mon, 21 Mar 2011 14:19:58 -0400, alok<> wrote:
    >>
    >>> if(getchar() != EOF)
    >>>
    >>> what is the value of EOF and for what keyborad input the if statement
    >>> will fail?

    >>
    >> The value of EOF is EOF, which is a negative value of type (int), which
    >> cannot be represented in an (unsigned char). The exact value is up to
    >> the implementation, and you should try not to know what it is, because
    >> if you know the value you're liable to write code which expects that value,
    >> and that code will fail when you try to run it on a different platform.

    >
    > Be wary anytime anyone tells you that you shouldn't know something. This is
    > very much the stance of the medieval Church.


    That isn't what Morris said and he explained the risks associated with
    assuming a value for EOF.
     
    Mark Bluemel, Mar 22, 2011
    #7
  8. alok

    Mark Bluemel Guest

    On 03/21/2011 06:19 PM, alok wrote:
    > if(getchar() != EOF)
    >
    > what is the value of EOF


    The language specification says
    " EOF
    .... expands to an integer constant expression, with type int and a
    negative value, that is returned by several functions to indicate
    end-of-file, that is, no more input from a stream"

    Note that the precise value is not specified - it is frequently -1
    but this is not required.

    > and for what keyborad input the if statement
    > will fail?


    This is not a question about the language, but about the platform.
    Different platforms have different ways of representing end-of-input
    on a keyboard. Common examples are ctrl-z (windows) and ctrl-d (default
    for at least some Unix/Linux shells). These values can be changed in
    some environments (for example with the stty command on Unix/Linux).

    Try reading Section 12 of the C FAQ at http://c-faq.com - you've more
    or less asked questions 12.1 and 12.1b
     
    Mark Bluemel, Mar 22, 2011
    #8
  9. In article <ima7eu$j21$-september.org>,
    Mark Bluemel <> wrote:
    >On 03/22/2011 12:37 PM, Kenny McCormack wrote:
    >> In article<op.vsplubdm5qv6o3@toshiba-laptop>,
    >> Morris Keesan<> wrote:
    >>> On Mon, 21 Mar 2011 14:19:58 -0400, alok<> wrote:
    >>>
    >>>> if(getchar() != EOF)
    >>>>
    >>>> what is the value of EOF and for what keyborad input the if statement
    >>>> will fail?
    >>>
    >>> The value of EOF is EOF, which is a negative value of type (int), which
    >>> cannot be represented in an (unsigned char). The exact value is up to
    >>> the implementation, and you should try not to know what it is, because
    >>> if you know the value you're liable to write code which expects that value,
    >>> and that code will fail when you try to run it on a different platform.

    >>
    >> Be wary anytime anyone tells you that you shouldn't know something. This is
    >> very much the stance of the medieval Church.

    >
    >That isn't what Morris said ...


    I think even the most experienced CLC word parser would be very hard-pressed
    to explain how Morris didn't say that you shouldn't know the value of EOF.

    --
    Just for a change of pace, this sig is *not* an obscure reference to
    comp.lang.c...
     
    Kenny McCormack, Mar 22, 2011
    #9
  10. On Mar 22, 2:37 pm, (Kenny McCormack)
    wrote:
    >
    > Be wary anytime anyone tells you that you shouldn't know something.  This is
    > very much the stance of the medieval Church.
    >

    Information hiding is a good ecclesiastical as well as programming
    principle.

    If you give people information they don't need they might misuse it,
    bringing down the entire system.
     
    Malcolm McLean, Mar 22, 2011
    #10
  11. On Tue, 22 Mar 2011 08:37:32 -0400, Kenny McCormack
    <> wrote:

    > In article <op.vsplubdm5qv6o3@toshiba-laptop>,
    > Morris Keesan <> wrote:
    >> On Mon, 21 Mar 2011 14:19:58 -0400, alok <> wrote:
    >>
    >>> if(getchar() != EOF)
    >>>
    >>> what is the value of EOF and for what keyborad input the if statement
    >>> will fail?

    >>
    >> The value of EOF is EOF, which is a negative value of type (int), which
    >> cannot be represented in an (unsigned char). The exact value is up to
    >> the implementation, and you should try not to know what it is, because
    >> if you know the value you're liable to write code which expects that
    >> value,
    >> and that code will fail when you try to run it on a different platform.

    >
    > Be wary anytime anyone tells you that you shouldn't know something.
    > This is
    > very much the stance of the medieval Church.
    >
    > If you want to know what EOF is on your system, try this:
    >
    > $ printf '#include <stdio.h>\nmain() {printf("EOF = %%d\\n",EOF);return
    > 0;}'|gcc -xc - && ./a.out && rm ./a.out
    >
    > which displays EOF = -1 on my system.


    And what would you do with this information, in any way which isn't
    likely to cause you problems?


    --
    Morris Keesan --
     
    Morris Keesan, Mar 22, 2011
    #11
  12. alok

    Tom St Denis Guest

    On Mar 22, 9:42 am, (Kenny McCormack)
    wrote:
    > In article <ima7eu$-september.org>,
    > Mark Bluemel  <> wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > >On 03/22/2011 12:37 PM, Kenny McCormack wrote:
    > >> In article<op.vsplubdm5qv6o3@toshiba-laptop>,
    > >> Morris Keesan<>  wrote:
    > >>> On Mon, 21 Mar 2011 14:19:58 -0400, alok<>  wrote:

    >
    > >>>> if(getchar() != EOF)

    >
    > >>>> what is the value of EOF and for what keyborad input the if statement
    > >>>> will fail?

    >
    > >>> The value of EOF is EOF, which is a negative value of type (int), which
    > >>> cannot be represented in an (unsigned char).  The exact value is upto
    > >>> the implementation, and you should try not to know what it is, because
    > >>> if you know the value you're liable to write code which expects that value,
    > >>> and that code will fail when you try to run it on a different platform.

    >
    > >> Be wary anytime anyone tells you that you shouldn't know something.  This is
    > >> very much the stance of the medieval Church.

    >
    > >That isn't what Morris said ...

    >
    > I think even the most experienced CLC word parser would be very hard-pressed
    > to explain how Morris didn't say that you shouldn't know the value of EOF..


    I don't see how knowing the exact value of EOF helps you in any way.

    Tom
     
    Tom St Denis, Mar 22, 2011
    #12
  13. On Tue, 22 Mar 2011 09:13:32 -0400, Mark Bluemel <>
    wrote:

    > On 03/22/2011 11:03 AM, pete wrote:
    >> Morris Keesan wrote:
    >>
    >>> What keyboard input generates an EOF is system-dependent,
    >>> and on the systems I most often write C code for,
    >>> the EOF character can be changed by the user.

    >>
    >> What's an "EOF character" ?
    >>

    > I took it as being shorthand for
    > "the keystroke (combination) which when entered on a keyboard
    > (or similar input device) indicates to the operating system
    > that end of file has been reached. This will be represented
    > by the C support libraries by delivering the EOF value as the
    > result of a read from this device."


    I intended it as shorthand for
    "the value stored in c_cc[VEOF] in an object of type (struct termios)",
    for which the description at
    <http://pubs.opengroup.org/onlinepubs/009695399/basedefs/termios.h.html>
    is "EOF character."
    --
    Morris Keesan --
     
    Morris Keesan, Mar 22, 2011
    #13
  14. On Mar 22, 4:49 pm, Tom St Denis <> wrote:
    >
    > I don't see how knowing the exact value of EOF helps you in any way.
    >

    Say you see

    while(ch = fgetc(fp) != -1)
    do_something(ch);

    The idiot programmer should have used EOF. But that's not the problem.
    You won't waste time changing -1 to EOF, recompiling and retesting,
    which might take a long time. You'll look elsewhere and hopefully spot
    what is actually wrong.
     
    Malcolm McLean, Mar 22, 2011
    #14
  15. In article <>,
    Malcolm McLean <> wrote:
    >On Mar 22, 2:37 pm, (Kenny McCormack)
    >wrote:
    >>
    >> Be wary anytime anyone tells you that you shouldn't know something.  This is
    >> very much the stance of the medieval Church.
    >>

    >Information hiding is a good ecclesiastical as well as programming
    >principle.
    >
    >If you give people information they don't need they might misuse it,
    >bringing down the entire system.


    Spoken like a good Catholic.

    The Pope would be proud of you.

    --
    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, Mar 22, 2011
    #15
  16. alok

    osmium Guest

    Tom St Denis wrote:

    > On Mar 22, 9:42 am, (Kenny McCormack)
    > wrote:
    >> In article <ima7eu$-september.org>,
    >> Mark Bluemel <> wrote:
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>> On 03/22/2011 12:37 PM, Kenny McCormack wrote:
    >>>> In article<op.vsplubdm5qv6o3@toshiba-laptop>,
    >>>> Morris Keesan<> wrote:
    >>>>> On Mon, 21 Mar 2011 14:19:58 -0400, alok<>
    >>>>> wrote:

    >>
    >>>>>> if(getchar() != EOF)

    >>
    >>>>>> what is the value of EOF and for what keyborad input the if
    >>>>>> statement will fail?

    >>
    >>>>> The value of EOF is EOF, which is a negative value of type (int),
    >>>>> which cannot be represented in an (unsigned char). The exact
    >>>>> value is up to
    >>>>> the implementation, and you should try not to know what it is,
    >>>>> because
    >>>>> if you know the value you're liable to write code which expects
    >>>>> that value, and that code will fail when you try to run it on a
    >>>>> different platform.

    >>
    >>>> Be wary anytime anyone tells you that you shouldn't know
    >>>> something. This is very much the stance of the medieval Church.

    >>
    >>> That isn't what Morris said ...

    >>
    >> I think even the most experienced CLC word parser would be very
    >> hard-pressed to explain how Morris didn't say that you shouldn't
    >> know the value of EOF.

    >
    > I don't see how knowing the exact value of EOF helps you in any way.


    But more fundamental, how does it *hurt* to know what it is?

    In a similar vein, I doubt if it helps to know pi to more than 20 places.
    But many people know how to find or even compute this value. Some people
    just want to know for the sake of knowing - I am one of those people. Of
    course they should be warned about the hazards of actually using the value.

    I liked the quote about the church; unfortunately I think there have been
    threads where the quote would have been a better fit rather than this
    particular thread.
     
    osmium, Mar 22, 2011
    #16
  17. alok

    Tom St Denis Guest

    On Mar 22, 1:07 pm, "osmium" <> wrote:
    > Tom St Denis wrote:
    > > On Mar 22, 9:42 am, (Kenny McCormack)
    > > wrote:
    > >> In article <ima7eu$-september.org>,
    > >> Mark Bluemel <> wrote:

    >
    > >>> On 03/22/2011 12:37 PM, Kenny McCormack wrote:
    > >>>> In article<op.vsplubdm5qv6o3@toshiba-laptop>,
    > >>>> Morris Keesan<> wrote:
    > >>>>> On Mon, 21 Mar 2011 14:19:58 -0400, alok<>
    > >>>>> wrote:

    >
    > >>>>>> if(getchar() != EOF)

    >
    > >>>>>> what is the value of EOF and for what keyborad input the if
    > >>>>>> statement will fail?

    >
    > >>>>> The value of EOF is EOF, which is a negative value of type (int),
    > >>>>> which cannot be represented in an (unsigned char). The exact
    > >>>>> value is up to
    > >>>>> the implementation, and you should try not to know what it is,
    > >>>>> because
    > >>>>> if you know the value you're liable to write code which expects
    > >>>>> that value, and that code will fail when you try to run it on a
    > >>>>> different platform.

    >
    > >>>> Be wary anytime anyone tells you that you shouldn't know
    > >>>> something. This is very much the stance of the medieval Church.

    >
    > >>> That isn't what Morris said ...

    >
    > >> I think even the most experienced CLC word parser would be very
    > >> hard-pressed to explain how Morris didn't say that you shouldn't
    > >> know the value of EOF.

    >
    > > I don't see how knowing the exact value of EOF helps you in any way.

    >
    > But more fundamental, how does it *hurt* to know what it is?


    Fine, but I wouldn't allow my developers to use -1 in place of EOF.

    > In a similar vein, I doubt if it helps to know pi to more than 20 places.


    Except, that pi by definition cannot change. EOF is defined as "a
    negative integer." it could be -2 for all it matters...

    > But many people know how to find or even compute this value.  Some people
    > just want to know for the sake of knowing - I am one of those people. Of
    > course they should be warned about the hazards of actually using the value.


    Then look it up, but don't presume that knowing the value of EOF helps
    you develop your application.

    > I liked the quote about the church; unfortunately I think there have been
    > threads where the quote would have been a better fit rather than this
    > particular thread.


    That's just Kenny trying to be grandios.

    Tom
     
    Tom St Denis, Mar 22, 2011
    #17
  18. In article <>,
    Tom St Denis <> wrote:
    ....
    >That's just Kenny trying to be grandios.


    That's just Tommy trying to be literate.

    --
    "We should always be disposed to believe that which appears to us to be
    white is really black, if the hierarchy of the church so decides."

    - Saint Ignatius Loyola (1491-1556) Founder of the Jesuit Order -
     
    Kenny McCormack, Mar 22, 2011
    #18
  19. (Kenny McCormack) writes:

    > Be wary anytime anyone tells you that you shouldn't know something.
    > This is very much the stance of the medieval Church.


    Can you elaborate on that?

    Personally, I don't see a problem in knowing the value of EOF. But
    unless you're implementing your own fgetc() for a particular platform,
    the knowledge is next to useless.

    And actually comparing with -1 (or whatever the particular
    implementation uses) instead of the symbol, is definitely a step down
    the road to portability trouble.

    --
    /Wegge

    Leder efter redundant peering af dk.*,linux.debian.*
     
    Anders Wegge Keller, Mar 22, 2011
    #19
  20. alok

    osmium Guest

    "Sherm Pendley" wrote:

    > "osmium" <> writes:
    >
    >> Tom St Denis wrote:
    >>
    >>> I don't see how knowing the exact value of EOF helps you in any way.

    >>
    >> But more fundamental, how does it *hurt* to know what it is?

    >
    > In general, I think it's poor practice to "look behind the curtain" of
    > whatever level of abstraction you happen to be working at. At any given
    > level, there are going to be implementation details that simply don't
    > matter to the task at hand, and concerning one's self with them only
    > serves as an unnecessary distraction.


    "EOF is often -1" has five words in it. Case closed. As a side effect it
    explains, it an elliptical way, why some functions return an int rather than
    a char.

    How many word are in the explanation about *why* it is poor practice?
     
    osmium, Mar 22, 2011
    #20
    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. Vasanth
    Replies:
    0
    Views:
    561
    Vasanth
    Jun 28, 2004
  2. Andrea Williams

    Clear the Render info & change to different info

    Andrea Williams, Oct 27, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,424
    Ken Dopierala Jr.
    Oct 27, 2004
  3. Kobu
    Replies:
    10
    Views:
    627
    Keith Thompson
    Mar 4, 2005
  4. SpreadTooThin

    ifstream eof not reporting eof?

    SpreadTooThin, Jun 13, 2007, in forum: C++
    Replies:
    10
    Views:
    697
    James Kanze
    Jun 15, 2007
  5. Jan Burse
    Replies:
    67
    Views:
    1,066
    Jan Burse
    Mar 14, 2012
Loading...

Share This Page