Thread: Getchar(), Keyboards, and EOF?

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    361

    Getchar(), Keyboards, and EOF?

    Since reading that fflush(stdin) is definitely not the way to go to clear the buffer for the keyboard (and if you don't know why, read the FAQ), I've moved over to the method of using:
    Code:
    while(((flush_char = getchar()) != '\n') && (flush_char != EOF));
    I'm just wondering though, under what case would getchar() return EOF from the keyboard's input?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    CTRL-Z (in DOS/win32-Console)
    CTRL-D (Linux)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Could it be dependant on the compiler I'm using? (Bloodshed Dev C++ here at school)

    When I press CTRL-Z all I get is a fanzy "^Z" being written to my screen.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It depends on the enviornment you're running under. You do whatever is defined on the keyboard to send EOF, and EOF gets sent to the program.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Okay, that makes sense, thanks alot

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I've moved over to the method of using
    That's a step in the right direction, but note that it's still a Band-Aid that covers up the problem rather than fixes it.

    >under what case would getchar() return EOF from the keyboard's input?
    End-of-file is set for the stream, or a read error occurs. End-of-file can usually be set through a control character such as Ctrl+Z or Ctrl+D. This is beyond the scope of C though, so we can't tell you for certain. Though typically Windows and DOS systems will use the former and POSIX systems will use the latter.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed