5

This may be a simple question, but i searched a lot and still didn't figure it out. I compiles below snip code by gcc and run program from terminal. In correct, It allow to enter an int and a char but it doesn't. It doesn't wait to enter the char??

Anyone here can help me will be kind. thanks in advance!

#include <stdio.h>

int main()
{

  char c;
  int i;

  // a
  printf("i: ");
  fflush(stdin); scanf("%d", &i);

  // b
  printf("c: ");
  fflush(stdin); scanf("%c", &c);

  return 0;

}

dangnam2910
  • 93
  • 1
  • 3
  • 7
  • 3
    `fflush(stdin)` gives you undefined behavior, don't do it. – Crowman Aug 25 '13 at 03:20
  • Did you give it an enter key as well as the input? – Arlie Stephens Aug 25 '13 at 03:20
  • 1
    fflush is not defined on the input stream. Too bad some C books actually encourage it. – digital_revenant Aug 25 '13 at 03:35
  • What does it give? The code snippet ends with scanf(), so nothing is being done with the variable `c`. Actually, the same for `i`. Note that scanf() parsing depends a lot on the `stty` settings of the terminal, if the input is a terminal. – ash Aug 25 '13 at 03:58

5 Answers5

8

%d will read consecutive digits until it encounters a non-digit. %c reads one character. Probably what's happening is that you're giving it a number (several digits) followed by a new line. %c then reads in that new line. You were probably intending for the fflush(stdin); to discard anything that hadn't yet been read, but unfortunately, that's undefined behavior.

The solution is to discard all whitespace before reading the character:

scanf(" %c", &c);

Note the space at the start. That means to discard all whitespace.

Community
  • 1
  • 1
icktoofay
  • 126,289
  • 21
  • 250
  • 231
2

You can use the getchar() to achieve what you want.

or consume the extra newline by using:-

 scanf(" %c", &c);
  ^^^   <------------Note the space

Reason:- Your next scanf for reading the character just reads/consumes the newline and hence never waits for user input

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

Instead of fflush(stdin); scanf("%c", &c);

1.use scanf with extra space

scanf(" %c",&c); 

or

2.use getchar() two times , first time reads '\n' which is entered after giving integer input and second time call ask you for give input as c:

getchar();
c=getchar();

would help you.

Gangadhar
  • 10,248
  • 3
  • 31
  • 50
1

First of all, scanf works when used as directed. I think the following code does what you want. Stdout is flushed so that user is prompted to enter an integer or a character. Using %1s allows white space like \n.

int main()
{

  char c[2];
  int i;

  printf("i: ");
  fflush(stdout);
  scanf("%d", &i);

  printf("c: ");
  fflush(stdout);
  scanf("%1s", &c);

  printf("\ni = %d, c = %c", i, c[0]);

  return 0;

}

This code was tested/run on an Eclipse/Microsoft C compiler.

JackCColeman
  • 3,777
  • 1
  • 15
  • 21
1

That fflush() is not guaranteed to do anything, and gcc/g++ doesn't. Not on Linux, anyway.

I thought I invented the following way to flush the rest of a line...until I saw it as an example in the ISO C spec (90 or 99...forgot which, but it's been there a long time either way...and I'll bet most readers here have seen it before.)

scanf("%*[^\n]%*c"); /* discard everything up to and including the next newline */

You can put that in your own "flush" function to save typing or pasting that all over the place.

You should still follow the suggestions to to put a space in scanf(" %c", &c);.

That will patiently wait for a non-whitespace character in case of a leading space or a double hit of the enter key.

Mike Housky
  • 3,959
  • 1
  • 17
  • 31