getchar() problem

Discussion in 'C Programming' started by Senthil-Raja, Jun 21, 2007.

  1. Senthil-Raja

    Senthil-Raja Guest

    The getchar() function is expected to fetch the next character in the
    input stream and return it.

    But, when I wrote a program using this function, it looks like the
    reading of the input stream happens only after I press the ENTER key.

    Please someone explain why this is happening.

    Regards,
    Senthil-Raja.
     
    Senthil-Raja, Jun 21, 2007
    #1
    1. Advertising

  2. Senthil-Raja

    Chris Dollin Guest

    Senthil-Raja wrote:

    > The getchar() function is expected to fetch the next character in the
    > input stream and return it.
    >
    > But, when I wrote a program using this function, it looks like the
    > reading of the input stream happens only after I press the ENTER key.
    >
    > Please someone explain why this is happening.


    Your input is line-buffered. This is common. See the FAQ.

    --
    Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    registered office: Berks RG12 1HN 690597 England
     
    Chris Dollin, Jun 21, 2007
    #2
    1. Advertising

  3. >>>>> "SR" == Senthil-Raja <> writes:

    SR> The getchar() function is expected to fetch the next character
    SR> in the input stream and return it.

    SR> But, when I wrote a program using this function, it looks like
    SR> the reading of the input stream happens only after I press the
    SR> ENTER key.

    SR> Please someone explain why this is happening.

    SR> Regards, Senthil-Raja.

    The short answer is buffering. Basically collects an entire line of
    input before it sends it to your program.

    You should also read question 19.1 in the c FAQ at c-faq.com.
    (at least I as I think thats the right question. I couldn't get
    c-faq.com to load and used an outdated copy at
    http://www.faqs.org/faqs/C-faq/faq/)
     
    Dale Henderson, Jun 21, 2007
    #3
  4. On Thu, 21 Jun 2007 20:02:53 +0530, in comp.lang.c , Senthil-Raja
    <> wrote:

    >The getchar() function is expected to fetch the next character in the
    >input stream and return it.


    .... after you press <enter> to terminate the line.

    >But, when I wrote a program using this function, it looks like the
    >reading of the input stream happens only after I press the ENTER key.


    thats how getchar() works. This is a FAQ.


    --
    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, Jun 21, 2007
    #4
  5. Senthil-Raja

    Army1987 Guest

    "Senthil-Raja" <> ha scritto nel messaggio news:1182436374.911542@slbhw0...
    > The getchar() function is expected to fetch the next character in the input stream and return it.
    >
    > But, when I wrote a program using this function, it looks like the reading of the input stream happens only after I press the
    > ENTER key.
    >
    > Please someone explain why this is happening.


    No. The stuff you type on your keyboard (or whatever input device)
    only reaches the input stream when you press ENTER. getchar() will
    read the characters as soon as possible.
    For example:

    #include <stdio.h>
    int main(void)
    {
    int ch;
    while (ch = getchar() > EOF)
    putchar(ch);
    putchar('\n');
    return 0;
    }
    will display *every* character you type, even if not immediately.
     
    Army1987, Jun 21, 2007
    #5
  6. Army1987 said:

    <snip>

    > For example:
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > int ch;
    > while (ch = getchar() > EOF)
    > putchar(ch);
    > putchar('\n');
    > return 0;
    > }
    > will display *every* character you type, even if not immediately.


    No, it won't. Finding out why not is left as an exercise.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Jun 21, 2007
    #6
  7. Senthil-Raja

    Army1987 Guest

    "Richard Heathfield" <> ha scritto nel messaggio news:...
    > Army1987 said:
    >
    > <snip>
    >
    >> For example:
    >>
    >> #include <stdio.h>
    >> int main(void)
    >> {
    >> int ch;
    >> while (ch = getchar() > EOF)

    while ((ch = getchar()) > EOF)

    >> putchar(ch);
    >> putchar('\n');
    >> return 0;
    >> }
    >> will display *every* character you type, even if not immediately.

    >
    > No, it won't. Finding out why not is left as an exercise.

    Darn it...
     
    Army1987, Jun 21, 2007
    #7
  8. Army1987 <> wrote:

    > while (ch = getchar() > EOF)

    while( (ch=getchar()) > EOF)
    > putchar(ch);


    > will display *every* character you type, even if not immediately.


    It will after you correctly account for operator precedence.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Jun 21, 2007
    #8
  9. In article <f5eph0$317$>,
    Christopher Benson-Manica <> wrote:

    > while( (ch=getchar()) > EOF)


    I suppose this is technically correct, modulo the usual question of
    what happens if sizeof(int)==1, but it's terrible style. The value of
    ch is either a legal character or EOF, so the natural idiom is

    while( (ch=getchar()) != EOF)

    Using > instead of != in this case is an insult to the reader.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Jun 21, 2007
    #9
  10. Senthil-Raja

    Eric Sosman Guest

    Christopher Benson-Manica wrote On 06/21/07 17:10,:
    > Army1987 <> wrote:
    >
    >
    >> while (ch = getchar() > EOF)

    >
    > while( (ch=getchar()) > EOF)
    >
    >> putchar(ch);

    >
    >
    >>will display *every* character you type, even if not immediately.

    >
    >
    > It will after you correctly account for operator precedence.


    Odd: I tried e corrected version and typed
    eleven aracters, but e program only displayed
    five (including e newline in bo counts). at
    caused is bug?

    --
     
    Eric Sosman, Jun 21, 2007
    #10
  11. In article <1182461637.78286@news1nwk>,
    Eric Sosman <> wrote:

    > Odd: I tried e corrected version and typed
    >eleven aracters, but e program only displayed
    >five (including e newline in bo counts). at
    >caused is bug?


    You need to be more careful with that stty command.

    As Eric is (I think) pointing out, an important reason for line
    buffering is that it allows the operating system to provide line
    editing, rather than requiring each program to do it.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Jun 21, 2007
    #11
  12. In article <f5ervq$3d3$>,
    Christopher Benson-Manica <> wrote:

    >Richard Tobin <> wrote:
    >
    >> I suppose this is technically correct, modulo the usual question of
    >> what happens if sizeof(int)==1, but it's terrible style. The value of
    >> ch is either a legal character or EOF, so the natural idiom is

    >
    >> while( (ch=getchar()) != EOF)

    >
    >> Using > instead of != in this case is an insult to the reader.

    >
    >I felt that > was unlikely to be correct, but I didn't feel confident
    >enough to make the change without hitting the references. My
    >apologies.


    My criticism was not aimed at you.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Jun 21, 2007
    #12
  13. Richard Tobin <> wrote:

    > I suppose this is technically correct, modulo the usual question of
    > what happens if sizeof(int)==1, but it's terrible style. The value of
    > ch is either a legal character or EOF, so the natural idiom is


    > while( (ch=getchar()) != EOF)


    > Using > instead of != in this case is an insult to the reader.


    I felt that > was unlikely to be correct, but I didn't feel confident
    enough to make the change without hitting the references. My
    apologies.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Jun 21, 2007
    #13
  14. Senthil-Raja

    Coos Haak Guest

    Op Thu, 21 Jun 2007 22:17:43 +0200 schreef Army1987:

    > "Richard Heathfield" <> ha scritto nel messaggio news:...
    >> Army1987 said:
    >>
    >> <snip>
    >>
    >>> For example:
    >>>
    >>> #include <stdio.h>
    >>> int main(void)
    >>> {
    >>> int ch;
    >>> while (ch = getchar() > EOF)

    > while ((ch = getchar()) > EOF)


    while ((ch = getchar()) != EOF)
    Consider signed chars and a char with value less than EOF ;-)

    --
    Coos
     
    Coos Haak, Jun 22, 2007
    #14
  15. In article <r4o5hmvbn7lj$.c5ny6gk43dhg$>,
    Coos Haak <> wrote:

    >>>> #include <stdio.h>
    >>>> int main(void)
    >>>> {
    >>>> int ch;
    >>>> while (ch = getchar() > EOF)

    >> while ((ch = getchar()) > EOF)


    > while ((ch = getchar()) != EOF)
    >Consider signed chars and a char with value less than EOF ;-)


    getchar() returns either EOF or the character "as an unsigned char
    converted to an int", so even if the character is negative as a signed
    char, the value returned by getchar() will be positive. Since EOF is
    negative, the code is correct. It's just unhelpful, because the
    natural reaction when reading it is to wonder what the significance of
    using < rather than != is, when in fact there is no significance to
    it.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Jun 22, 2007
    #15
  16. Senthil-Raja

    Coos Haak Guest

    Op 21 Jun 2007 23:24:38 GMT schreef Richard Tobin:

    > In article <r4o5hmvbn7lj$.c5ny6gk43dhg$>,
    > Coos Haak <> wrote:
    >
    >>>>> #include <stdio.h>
    >>>>> int main(void)
    >>>>> {
    >>>>> int ch;
    >>>>> while (ch = getchar() > EOF)
    >>> while ((ch = getchar()) > EOF)

    >
    >> while ((ch = getchar()) != EOF)
    >>Consider signed chars and a char with value less than EOF ;-)

    >
    > getchar() returns either EOF or the character "as an unsigned char
    > converted to an int", so even if the character is negative as a signed
    > char, the value returned by getchar() will be positive.

    Understood.
    > Since EOF is
    > negative, the code is correct. It's just unhelpful, because the
    > natural reaction when reading it is to wonder what the significance of
    > using < rather than != is, when in fact there is no significance to
    > it.

    Agreed.
    --
    Coos
     
    Coos Haak, Jun 22, 2007
    #16
  17. Senthil-Raja

    CBFalconer Guest

    Coos Haak wrote:
    > Op Thu, 21 Jun 2007 22:17:43 +0200 schreef Army1987:
    >> "Richard Heathfield" <>
    >>> Army1987 said:
    >>>
    >>> <snip>
    >>>
    >>>> #include <stdio.h>
    >>>> int main(void)
    >>>> {
    >>>> int ch;
    >>>> while (ch = getchar() > EOF)

    >> while ((ch = getchar()) > EOF)

    >
    > while ((ch = getchar()) != EOF)
    > Consider signed chars and a char with value less than EOF ;-)


    No such thing. Consult the standard definition of getchar().

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    <http://www.securityfocus.com/columnists/423>
    <http://www.aaxnet.com/editor/edit043.html>
    cbfalconer at maineline dot net



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Jun 22, 2007
    #17
  18. Senthil-Raja

    CBFalconer Guest

    Richard Tobin wrote:
    > In article <f5eph0$317$>,
    > Christopher Benson-Manica <> wrote:
    >
    >> while( (ch=getchar()) > EOF)

    >
    > I suppose this is technically correct, modulo the usual question
    > of what happens if sizeof(int)==1, but it's terrible style. The
    > value of ch is either a legal character or EOF, so the natural
    > idiom is
    >
    > while( (ch=getchar()) != EOF)
    >
    > Using > instead of != in this case is an insult to the reader.


    No it isn't. It is not the usual, but may even improve the actual
    code in some instances. The insult is not installing spaces around
    the operators, if we must have an insult somewhere :)

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    <http://www.securityfocus.com/columnists/423>
    <http://www.aaxnet.com/editor/edit043.html>
    cbfalconer at maineline dot net



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Jun 22, 2007
    #18
  19. Senthil-Raja

    Army1987 Guest

    "Coos Haak" <> ha scritto nel messaggio news:r4o5hmvbn7lj$.c5ny6gk43dhg$...
    > Op Thu, 21 Jun 2007 22:17:43 +0200 schreef Army1987:
    >
    >> "Richard Heathfield" <> ha scritto nel messaggio news:...
    >>> Army1987 said:
    >>>
    >>> <snip>
    >>>
    >>>> For example:
    >>>>
    >>>> #include <stdio.h>
    >>>> int main(void)
    >>>> {
    >>>> int ch;
    >>>> while (ch = getchar() > EOF)

    >> while ((ch = getchar()) > EOF)

    >
    > while ((ch = getchar()) != EOF)
    > Consider signed chars and a char with value less than EOF ;-)

    Consider reading what the standard says about fgetc(), and hence
    about getc(), and hence about getchar().
    (Yes, that is a problem on systems where UCHAR_MAX > INT_MAX, but
    on these systems there will be an unsigned char which when
    converted to int will compare equal to EOF, so even your fix
    doesn't help there.)

    #include <limits.h>
    while ( (ch = getchar()) != EOF
    #if UCHAR_MAX > INT_MAX
    || !feof(stdin) && !ferror(stdin)
    #endif
    )
     
    Army1987, Jun 22, 2007
    #19
  20. Senthil-Raja

    Flash Gordon Guest

    Dale Henderson wrote, On 21/06/07 16:01:

    <snip>

    > You should also read question 19.1 in the c FAQ at c-faq.com.
    > (at least I as I think thats the right question. I couldn't get
    > c-faq.com to load and used an outdated copy at
    > http://www.faqs.org/faqs/C-faq/faq/)


    http://c-faq.com/ works for me and when possibly it is the link people
    should use.
    --
    Flash Gordon
     
    Flash Gordon, Jun 23, 2007
    #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. Replies:
    21
    Views:
    1,212
    Richard Herring
    Apr 20, 2005
  2. Looking for Help

    Input problem with getchar()

    Looking for Help, Aug 25, 2006, in forum: C Programming
    Replies:
    1
    Views:
    679
    sisovich
    Sep 6, 2006
  3. scanf/getchar sequence problem

    , Apr 7, 2005, in forum: C Programming
    Replies:
    21
    Views:
    4,777
    Richard Herring
    Apr 20, 2005
  4. broeisi

    getchar function and EOF problem..

    broeisi, Mar 10, 2006, in forum: C Programming
    Replies:
    13
    Views:
    622
    Barry Schwarz
    Mar 12, 2006
  5. Alan

    getchar() problem

    Alan, May 11, 2006, in forum: C Programming
    Replies:
    6
    Views:
    638
    Peter Shaggy Haywood
    May 14, 2006
Loading...

Share This Page