[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: off topic: Doubt about gcc 3.3.3 and ( getchar() or scanf(%s) )?

On Tue, 12 Oct 2004, Vinicius wrote:

> Hello,
> the following code does not work:
> "
> ...
> void main(void)
> {
>   char option;
>   printf("Choose an option: ");
>   option = toupper(getchar());
>   printf("Chosse another option: ");
>   scanf("%c", &option);
>   ...
> It shows "Choose an option: Choose another option ". Why?
> Whether I put two lines "fflush(stdin)" before the inputs, the same
> above occurs.

What were you expecting?

(1) main() must return an int.

(2) getchar() reads a single character from the input stream, but before
you can see any output, you must enter the return.  That character sits in
the input buffer and is read by the scanf() call.

(3) It doesn't make sense to call fflush() on a stream that is not an
output or update stream.  See "man fflush".

> my gcc: "$ gcc --version
> gcc (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)"
> TIA, Vinicius.
> P.S.: Where is the best place to put this question, pĺease?

Any decent C book should discuss the relation between stream input and
output and the console and the keyboard.  Or Google for the comp.lang.c

		Matthew Saltzman

Clemson University Math Sciences
mjs AT clemson DOT edu

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]