0

i have an issue with two pieces of code which are nearly the same, but i don't know why they don't behave the same.

Here is the first one :

printf("Type something : );
scanf("%d", &nb);
scanf("%c", &c);

Here is the second one :

printf("Type something : );
scanf("%d", &nb);
scanf("%c", &c);
fgetc(stdin);

In the first case, the program still waits for an output at the end.

In the second one, the program skips the second scanf instruction.

It seems quite unclear to me. Can you help me please ?

Thanks in advance.

Thomas
  • 11
  • 4

1 Answers1

0

It seems your explanation is reversed. The first program may seem to skip the second scanf because any spaces or newlines followed by a sequence of digits may be read instead. But the second program will wait for your input because of the call fgetc(stdin).

Simon Woo
  • 474
  • 3
  • 5