James wrote:
> ok, compile this code, then explain why it does what it does...
>
> /* Start Of Code */
> #include <stdio.h>
>
> char c;
This should be `int c;'. It won't make any difference if 0 <= c < 128,
but if c >= 128 or c < 0 (e.g. if c == EOF) you will get the wrong
results.
> int main ()
> {
> printf ("Scanf Test\n");
> printf ("enter a character >");
You should really call `fflush(stdout)' here; reading from an input
stream which is associated with a terminal device will flush all
output buffers, but if stdin is a pipe/socket, the line won't be
displayed.
> scanf ("%c", &c);
> printf ("Char was [%c]\n", c);
> printf ("Getc test\n");
> printf ("Enter a character >");
> c = getc (stdin);
> printf ("Char was [%c]\n", c);
>
> return 0;
> }
> /* End of Code */
>
> What does it do? It will let me input the first char, but never asks for the
> second. Why? and how can i make it ask for the second (or however many i ask
> it for).
>
> incidentally, i've seen EXACTLY the same 'feature' in Assembly, Pascal and
> Modula 2!
I think that you're forgetting two things:
a) that the stdio functions are buffered. If stdin/stdout correspond
to a TTY device, they are line buffered by default, otherwise they are
fully buffered. stderr is always unbuffered. To change the buffering
mode of a stream, use setvbuf().
b) the TTY driver is in canonical (ICANON) mode by default, which
means that it sends data to the process a line at a time. If you want
to read individual characters as they are typed, you will need to use
tcsetattr() to put the terminal into raw mode.
--
Glynn Clements <[EMAIL PROTECTED]>