Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > getchar() problem

Reply
Thread Tools

getchar() problem

 
 
Senthil-Raja
Guest
Posts: n/a
 
      06-21-2007
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.
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      06-21-2007
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

 
Reply With Quote
 
 
 
 
Dale Henderson
Guest
Posts: n/a
 
      06-21-2007
>>>>> "SR" == Senthil-Raja <(E-Mail Removed)> 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/)

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      06-21-2007
On Thu, 21 Jun 2007 20:02:53 +0530, in comp.lang.c , Senthil-Raja
<(E-Mail Removed)> 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
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      06-21-2007

"Senthil-Raja" <(E-Mail Removed)> 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.



 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-21-2007
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.
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      06-21-2007

"Richard Heathfield" <(E-Mail Removed)> ha scritto nel messaggio news:(E-Mail Removed)...
> 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...


 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      06-21-2007
Army1987 <(E-Mail Removed)> 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.
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      06-21-2007
In article <f5eph0$317$(E-Mail Removed)>,
Christopher Benson-Manica <(E-Mail Removed)> 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.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-21-2007
Christopher Benson-Manica wrote On 06/21/07 17:10,:
> Army1987 <(E-Mail Removed)> 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?

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments