why (getchar() != EOF) always equal to 1

Discussion in 'C Programming' started by arnuld, Mar 8, 2007.

  1. arnuld

    arnuld Guest

    #include <stdio.h>

    int main() {

    printf("%d\n", getchar() != EOF);

    return 0;
    }

    when i run this programme, no matter what i give it as input, output
    is always equal to 1, never 0 (zero) or 5

    why ?
    arnuld, Mar 8, 2007
    #1
    1. Advertising

  2. arnuld

    Mike Wahler Guest

    "arnuld" <> wrote in message
    news:...
    > #include <stdio.h>
    >
    > int main() {
    >
    > printf("%d\n", getchar() != EOF);
    >
    > return 0;
    > }
    >
    > when i run this programme, no matter what i give it as input, output
    > is always equal to 1, never 0 (zero) or 5


    Did you supply whatever your console defines as end-of-file?

    For example, Microsoft Windows uses ctrl-Z, UNIX uses ctrl-D,
    etc.


    >
    > why ?


    You've probably not sent the proper keystroke(s) for your
    operating system. Consult your documentation.

    -Mike
    Mike Wahler, Mar 8, 2007
    #2
    1. Advertising

  3. arnuld

    Ian Collins Guest

    arnuld wrote:
    > #include <stdio.h>
    >
    > int main() {
    >
    > printf("%d\n", getchar() != EOF);
    >
    > return 0;
    > }
    >
    > when i run this programme, no matter what i give it as input, output
    > is always equal to 1, never 0 (zero) or 5
    >

    Well if you type whatever the end of file character on you system is
    (ctrl-D on Unix like systems) it will be 0.

    A boolean expression can only be 0 or 1. Where did you get 5 from?

    --
    Ian Collins.
    Ian Collins, Mar 8, 2007
    #3
  4. arnuld

    arnuld Guest

    > On Mar 8, 11:42 am, Ian Collins <> wrote:


    > Well if you type whatever the end of file character on you system is
    > (ctrl-D on Unix like systems) it will be 0.



    ok. i use Linux, EOF character is an integer "-1". if i enter "-1"
    output is still "1"
    but if i type "Ctrl-D" then output is "0" (zero).


    > A boolean expression can only be 0 or 1. Where did you get 5 from?


    i was confused actually.

    EOF = -1

    on my system (Arch Linux i686, gcc 4.1.2)

    BTW, if EOF is defined as an integer inside <stdio.h>, then why do i
    get "1" when i give "-1" as input ?

    > Ian Collins.
    arnuld, Mar 8, 2007
    #4
  5. arnuld

    dwks Guest

    On Mar 7, 11:48 pm, "arnuld" <> wrote:
    > > On Mar 8, 11:42 am, Ian Collins <> wrote:
    > > Well if you type whatever the end of file character on you system is
    > > (ctrl-D on Unix like systems) it will be 0.

    >
    > ok. i use Linux, EOF character is an integer "-1". if i enter "-1"
    > output is still "1"
    > but if i type "Ctrl-D" then output is "0" (zero).
    >
    > > A boolean expression can only be 0 or 1. Where did you get 5 from?

    >
    > i was confused actually.
    >
    > EOF = -1
    >
    > on my system (Arch Linux i686, gcc 4.1.2)
    >
    > BTW, if EOF is defined as an integer inside <stdio.h>, then why do i
    > get "1" when i give "-1" as input ?
    >
    > > Ian Collins.


    Look at your code.
    > #include <stdio.h>
    >
    > int main() {
    >
    > printf("%d\n", getchar() != EOF);
    >
    > return 0;
    >
    > }

    You're printing a boolean value, like "1 == 0", which can be either
    true or false. Most compilers use 1 for true and nearly all use 0 for
    false. So that's what you get printed.

    If you did this, on the other hand:

    #include <stdio.h>

    int main(void) {
    printf("%d\n", getchar());
    }

    (leaving out the !=EOF), you would get (for an ASCII system) 48 if you
    typed '0', 65 if you typed 'a', etc; the ASCII code for whatever
    character you typed.
    dwks, Mar 8, 2007
    #5
  6. "arnuld" <> writes:
    >> On Mar 8, 11:42 am, Ian Collins <> wrote:
    >> Well if you type whatever the end of file character on you system is
    >> (ctrl-D on Unix like systems) it will be 0.

    >
    >
    > ok. i use Linux, EOF character is an integer "-1". if i enter "-1"
    > output is still "1"
    > but if i type "Ctrl-D" then output is "0" (zero).


    No, the EOF character is not "-1".

    EOF is (a macro that expands to) an integer value; it's not a
    character. The getchar() function returns this value when it reaches
    the end of the input file, or on an error; otherwise, it returns the
    integer value of the input character. getchar() does *not* convert
    strings of decimal digits to their numeric value; there are other
    functions that will do that.

    >> A boolean expression can only be 0 or 1. Where did you get 5 from?


    Actually, a boolean expression is either zero or non-zero -- but the
    result of a relational or equality operator is always 0 or 1.

    > i was confused actually.
    >
    > EOF = -1
    >
    > on my system (Arch Linux i686, gcc 4.1.2)
    >
    > BTW, if EOF is defined as an integer inside <stdio.h>, then why do i
    > get "1" when i give "-1" as input ?


    When you give "-1" as input, you're entering a '-' character followed
    by a '1' character. If your system uses the ASCII character set, the
    '-' character will cause getchar() to return the value 45; the '1'
    character will cause it to return the value 49. Other character sets
    will result in other values.

    Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 8, 2007
    #6
  7. "dwks" <> writes:
    > On Mar 7, 11:48 pm, "arnuld" <> wrote:

    [...]
    > Look at your code.
    >> #include <stdio.h>
    >>
    >> int main() {
    >>
    >> printf("%d\n", getchar() != EOF);
    >>
    >> return 0;
    >>
    >> }

    > You're printing a boolean value, like "1 == 0", which can be either
    > true or false. Most compilers use 1 for true and nearly all use 0 for
    > false. So that's what you get printed.

    [...]

    Most? Nearly?

    When an integer expression is used as a condition, the value 0 is
    treated as false, and any non-zero value is treated as true.

    The result of any relational ("<", "<=", ">", ">=") or equality ("==",
    "!=") operator is *always* 0 for false, 1 for true.

    Any compiler that gets this wrong is broken. (I've never heard of a C
    compiler that was broken in that particular way.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 8, 2007
    #7
  8. arnuld

    Flash Gordon Guest

    arnuld wrote, On 08/03/07 06:48:
    >> On Mar 8, 11:42 am, Ian Collins <> wrote:

    >
    >> Well if you type whatever the end of file character on you system is
    >> (ctrl-D on Unix like systems) it will be 0.

    >
    > ok. i use Linux, EOF character is an integer "-1". if i enter "-1"
    > output is still "1"
    > but if i type "Ctrl-D" then output is "0" (zero).
    >
    >> A boolean expression can only be 0 or 1. Where did you get 5 from?


    <snip>

    > BTW, if EOF is defined as an integer inside <stdio.h>, then why do i
    > get "1" when i give "-1" as input ?


    getchar gets a character. How many characters did you type when you
    typed "-1"? Was any of those characters a signal that the end of file
    has been reached?

    It would make as much sense to ask why you email client did not close
    its "compose" window when you typed in -1.

    Now go and read the comp.lang.c FAQ at http://c-faq.com/
    Question 12.1b specifically deals with your problem. Please always check
    the FAQ before posting, it has answers to a lot of the easy questions so
    you will get your answer a lot faster as well as avoiding the same
    questions being asked over and over again. If you don't understand
    something in the FAQ or cannot find the answer then by all means ask for
    clarification, but in this case it is so obviously this question the
    only way you could have missed it is to have not looked.
    --
    Flash Gordon
    Flash Gordon, Mar 8, 2007
    #8
  9. arnuld

    santosh Guest

    arnuld wrote:
    > > On Mar 8, 11:42 am, Ian Collins <> wrote:

    >
    >
    > > Well if you type whatever the end of file character on you system is
    > > (ctrl-D on Unix like systems) it will be 0.

    >
    >
    > ok. i use Linux, EOF character is an integer "-1".


    The C standard does not specify the value of the macro EOF, just that
    it expands to a value of type int that is the implementation's choice
    for specifying end-of-file conditions. It's commonly -1, but that's
    not a strictly portable assumption.

    > if i enter "-1"
    > output is still "1"
    > but if i type "Ctrl-D" then output is "0" (zero).


    All input is treated as a sequence of characters. The character
    sequence "-1" expands to the value specified in the system's character
    set for '-' followed by that for '1'. Otherwise you will not be able
    to enter "-1" when you want to do so. Signalling end-of-file, among
    others is a control sequence. It's not meant to interfere with the
    values assigned for normal input, hence special key sequences are
    assigned to generate such values. Under UNIX systems it's commonly
    generated by the key sequence 'Ctrl-d', (hold down the Control key and
    press 'd'). Under Windows it's 'Ctrl-z'

    > BTW, if EOF is defined as an integer inside <stdio.h>, then why do i
    > get "1" when i give "-1" as input ?


    That's because to specify it from the keyboard, special key sequences
    are needed to prevent it from interfering with normal input values.
    Similarly, the system's end-of-line can be generated by Ctrl-m under
    Unices.
    santosh, Mar 8, 2007
    #9
  10. On 7 Mar 2007 22:48:23 -0800, in comp.lang.c , "arnuld"
    <> wrote:

    >> On Mar 8, 11:42 am, Ian Collins <> wrote:

    >
    >
    >> Well if you type whatever the end of file character on you system is
    >> (ctrl-D on Unix like systems) it will be 0.

    >
    >
    >ok. i use Linux, EOF character is an integer "-1". if i enter "-1"
    >output is still "1"


    You're confusing the EOF flag with the EOF signal.

    >but if i type "Ctrl-D" then output is "0" (zero).


    Crtl-D sends an end-of-file signal to your application. This causes
    some functions to return a magic value which is typically -1.
    >
    >BTW, if EOF is defined as an integer inside <stdio.h>, then why do i
    >get "1" when i give "-1" as input ?


    Because you told it to print the result of
    "getchar is not equal to EOF"

    which will be 1 (true) when getchar returned EOF, and zero otherwise.
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Mark McIntyre, Mar 8, 2007
    #10
  11. On 8 Mar 2007 03:23:50 -0800, in comp.lang.c , "santosh"
    <> wrote:

    >arnuld wrote:
    >> > On Mar 8, 11:42 am, Ian Collins <> wrote:

    >>
    >>
    >> > Well if you type whatever the end of file character on you system is
    >> > (ctrl-D on Unix like systems) it will be 0.

    >>
    >>
    >> ok. i use Linux, EOF character is an integer "-1".

    >
    >The C standard does not specify the value of the macro EOF, just that
    >it expands to a value of type int that is the implementation's choice
    >for specifying end-of-file conditions.


    And must be negative (7.19.1 p3)

    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Mark McIntyre, Mar 8, 2007
    #11
  12. Mark McIntyre <> writes:
    > On 7 Mar 2007 22:48:23 -0800, in comp.lang.c , "arnuld"
    > <> wrote:
    >>> On Mar 8, 11:42 am, Ian Collins <> wrote:

    >>
    >>> Well if you type whatever the end of file character on you system is
    >>> (ctrl-D on Unix like systems) it will be 0.

    >>
    >>ok. i use Linux, EOF character is an integer "-1". if i enter "-1"
    >>output is still "1"

    >
    > You're confusing the EOF flag with the EOF signal.
    >
    >>but if i type "Ctrl-D" then output is "0" (zero).

    >
    > Crtl-D sends an end-of-file signal to your application. This causes
    > some functions to return a magic value which is typically -1.


    I presume you're using "signal" in its colloquial English sense.
    Since C does have its own concept of "signals" (see <signal.h>, C99
    7.14), it's important to note that an end-of-file condition does not
    normally cause a signal (SIGWHATEVER) to be delivered to the program.
    It does "signal" the program by causing getchar() to return the value
    of the EOF macro.
    [...]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 9, 2007
    #12
  13. On Thu, 08 Mar 2007 16:08:42 -0800, in comp.lang.c , Keith Thompson
    <> wrote:

    >Mark McIntyre <> writes:
    >> On 7 Mar 2007 22:48:23 -0800, in comp.lang.c , "arnuld"
    >> <> wrote:
    >>>> On Mar 8, 11:42 am, Ian Collins <> wrote:
    >>>

    >> Crtl-D sends an end-of-file signal to your application. This causes
    >> some functions to return a magic value which is typically -1.

    >
    >I presume you're using "signal" in its colloquial English sense.


    correct, I doubt anyone was confused but for clarity, I am indeed not
    referrring to things found in signal.h
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Mark McIntyre, Mar 9, 2007
    #13
    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. Martin Dickopp

    Re: EOF and getchar/fgetc

    Martin Dickopp, Feb 14, 2004, in forum: C Programming
    Replies:
    0
    Views:
    2,151
    Martin Dickopp
    Feb 14, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,693
    Smokey Grindel
    Dec 2, 2006
  3. Luke Wu

    The need for int to capture getchar()'s EOF

    Luke Wu, Jan 23, 2005, in forum: C Programming
    Replies:
    6
    Views:
    573
    Lawrence Kirby
    Jan 26, 2005
  4. broeisi

    getchar function and EOF problem..

    broeisi, Mar 10, 2006, in forum: C Programming
    Replies:
    13
    Views:
    599
    Barry Schwarz
    Mar 12, 2006
  5. Replies:
    17
    Views:
    506
    Dik T. Winter
    Nov 21, 2007
Loading...

Share This Page