By Xisso2


2015-03-10 03:19:03 8 Comments

char c;
char s[32];
puts("Type a char");
c=getchar();
fflush(stdin);
puts("Type a string");
fgets(s,32,stdin);

Without the fflush(), if you type a character, say "a", and the hit enter, the input buffer contains "a\n", the getchar() peeks the "a", but the "\n" remains in the buffer, so the next fgets() will find it and return an empty string without even waiting for user input.

What should be done instead? As far as i know, it's not defined or standard to use fflush() on an input stream?

3 comments

@Basile Starynkevitch 2015-03-10 06:25:29

Read about fflush(3); you probably want to call fflush(NULL);before c=getchar(); but that might not work as you want.

Remember that on Linux at least the terminal is often kernel buffered (and also stdio buffered). Read more about tty-s. So you often won't get any input till the user pressed the enter key.

You should rethink your program and read entire lines (often with getline(3) or fgets(3) on a large enough buffer).

If you want to get individual key presses on Linux (or other POSIX systems) you need a library like ncurses (which works with the terminal in raw mode). You might be interested in readline(3)

@M.M 2015-03-10 06:17:33

If you are trying to discard the rest of the input line, then do this:

int ch;
while ( (ch = getchar()) != '\n' && ch != EOF) { }

This reads characters until there are no more, or it hits the end of the line.

Note that you should be using int c; rather than char c; . The getchar() function actually does not return a char value; it returns the result of converting that char to unsigned char.

@R.. GitHub STOP HELPING ICE 2015-03-10 03:21:59

Don't use getchar when your intent is to read a line of input and interpret one character from it. In this case, read a line with fgets or similar and just inspect the first line.

Alternatively, you can stick with getchar, but then you need to keep reading characters until the end of the line and throw them away before moving on.

Your program could be transformed to (first approach):

char c;
char s[32];
puts("Type a char");
fgets(s,32,stdin);
c=s[0];
puts("Type a string");
fgets(s,32,stdin);

Note that this is missing checking of return values and doesn't handle input longer than 32 bytes, but it's a start and those are separate issues you can work on.

@Xisso2 2015-03-10 03:25:57

Okej thanks, but what should be done in this example: ´ printf("hp: "); scanf("%f",&acourse.hp); fflush(stdin); printf("Grade: "); scanf("%c",&acourse.grade);´

@R.. GitHub STOP HELPING ICE 2015-03-10 03:29:53

In the case of scanf where you want to read a whole line but just interpret one field from it, something like this works: scanf("%f%*[^\n]%*c", &x); Alternatively (most people prefer the latter), use fgets then sscanf rather than using scanf.

@Xisso2 2015-03-10 03:30:53

Thanks again! Helped

@David C. Rankin 2015-03-10 03:34:26

@Xisso2 note fflush(stdin); does not apply to input streams. It will return EBADF error and not do what you think it does. See man 3 fflush

@R.. GitHub STOP HELPING ICE 2015-03-10 03:54:38

@DavidC.Rankin: fflush on an input stream results in undefined behavior, not a diagnosable error. POSIX fills in this gap and defines the behavior for seekable input streams, but not non-seekable ones, which are still left undefined.

@David C. Rankin 2015-03-10 04:01:29

I see the distinction, EBADF can tell you that its failure was due to being run on a stream not for writing, but being run on stdin will not necessarily set EBADF. Thanks for pointing out the difference.

Related Questions

Sponsored Content

22 Answered Questions

[SOLVED] What is the "-->" operator in C++?

27 Answered Questions

22 Answered Questions

[SOLVED] How do you read from stdin?

  • 2009-09-20 05:48:07
  • tehryan
  • 1555889 View
  • 1419 Score
  • 22 Answer
  • Tags:   python stdin

4 Answered Questions

[SOLVED] What does the C ??!??! operator do?

  • 2011-10-19 16:56:59
  • Peter Olson
  • 255710 View
  • 1951 Score
  • 4 Answer
  • Tags:   c operators trigraphs

5 Answered Questions

[SOLVED] What is ":-!!" in C code?

3 Answered Questions

[SOLVED] How can I clear stdin in a C progam?

  • 2018-10-03 04:45:32
  • xjka
  • 677 View
  • 3 Score
  • 3 Answer
  • Tags:   c stdin

1 Answered Questions

[SOLVED] fflush() function not working with stdin

  • 2014-10-19 01:23:10
  • harianja
  • 262 View
  • 1 Score
  • 1 Answer
  • Tags:   c stdin fflush

5 Answered Questions

[SOLVED] C, flushing stdin

  • 2010-10-06 19:16:32
  • DoggyDoo
  • 4099 View
  • 4 Score
  • 5 Answer
  • Tags:   c stdin

Sponsored Content