On Sat, Apr 27, 2002 at 01:53:56AM +0300, Hetz Ben Hamo wrote:
> 
> #include <stdio.h>
> void main()
> {
>       char ch,let;

int ch,let;

Otherwise you can't check for errors from getchar(). Even if you 
don't intend to check the error, getchar() return an int so you
should store it as an int. Throwing away information implicitly
like this is risky (just like void main, really).

>       ch=getchar();
>       flushall();
>       let=getchar();
>       putchar(let);
>       putchar(ch);
> }
> 
> the flushall function is not known in GCC, 

AFAIK it's not known in C either and is an MS extension. 

According to glibc's man page fflush(NULL) flushes all buffers, but
I'm not sure if that's ANSI. It's not really necessary, either, 
because you usually know what file you want to flush (or you have 
bigger problems :)

> but the fflush is, and I can use fflush(stdin), 

No. fflush() is for output buffers. You don't flush input buffers
just like you don't flush a water tap. I don't know exactly what 
flushall() is or what it is supposed to do.

> but that doesn't work (feel free to compile and test it), as 
> the ch coming always sort-of empty (decimal value 10)..
> 
> Suggestions?

What are you trying to achieve? In UNIX and Linux, TTY input
is by default buffered not only at the libc level but also
at the kernel level: when you type at the terminal, you type
whole lines and the kernel lets you line-edit those. Only when
you press enter the entire line goes to the reading 
application. So you can't really read the first characters from
a line that hasn't been completely typed yet. getchar() before 
the entire line was typed would just block until you press enter.
There is no portable way in C to assure reading of individual
characters.

In UNIX, The way to bypass the kernel's line editing and get 
characters is to switch the TTY into "raw mode", but that's 
grossly unportable. UNIX TTYs are meant for command lines.

If you type "10<enter>" the program should output "01" if that's
what you want, regardless of whether you flush or not, but the
output won't be printed until the kernel gets the newline. No
newline, no soup. 

See the C FAQ (a great resource).

=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to