6

what is the purpose of putting a space in scanf like this

scanf(" %c",&ch) 

in place of

scanf("%c",&ch)?

Also what is input buffer in fflush(stdin)?

taocp
  • 23,276
  • 10
  • 49
  • 62
Rahul Kathuria
  • 129
  • 2
  • 7
  • Putting a space there will ignore any sort of whitespace before reading the character. – SBI Jun 14 '13 at 14:50
  • `stdin`, inputstream of your program..... whatever you input through your console will be placed in that buffer – pinkpanther Jun 14 '13 at 14:52
  • 1
    `fflush()` is for flushing _output_ buffers, so calling it on an input stream is meaningless (it's usually due to a misunderstanding that it will discard any input that is already buffered). – Barmar Jun 14 '13 at 14:54
  • Um, this is explained right there in the documentation. No need to ask SO. – Raymond Chen Jun 14 '13 at 15:00
  • @barmar, which even if it does is terrible. Think of a file redirected as input. You may think you are flushing the last entered line, but in reality you are ignoring the whole input that is provided by that redirection. flushing input is plain meaningless. If you don't want to read the input, just don't! – Shahbaz Jun 14 '13 at 15:01

6 Answers6

4

Because the space before %c ignores all whitespace. *scanf family of functions ignore all whitespace before any % by default except for %c, %[ and %n. This is mentioned in C11 at:

7.21.6.2.8

Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier.

To be complete, here's the part that says all whitespace will be ignored:

7.21.6.2.5

A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read. The directive never fails.


Regarding your second question, fflush(stdin) causes undefined behavior and must not be used (emphasis mine):

7.21.5.2.2

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • Thanks everybody but even if I flush input stream,will it flush the whole input stream or just the white spaces or newline characters? Also When i would realize that i have to remove white spaces in an input stream of characters or i have to explicitly do that in every reading of characters? – Rahul Kathuria Jun 14 '13 at 16:00
  • @RahulKathuria, you never flush input streams. If your OS formats your hard disk as a result, it's completely your fault. If it tries to be nice, it would simply throw away all your input, so you have nothing to read anymore. So, just don't flush input streams, ok? – Shahbaz Jun 14 '13 at 19:11
  • Second, `*scanf` skips whitespace for you, so no choice there. The only place you get a choice would be with `%c` (I doubt you'd use `%[`). There, if you are interested in reading whitespace, put a space before it (`" %c"`). If you are not interested in whitespace, don't put a space before it (`"%c"`). It's as easy as that. The rest would be managed by `scanf`. – Shahbaz Jun 14 '13 at 19:13
3

what is the purpose of putting a space in scanf like this scanf(" %c",&ch) in place of scanf("%c",&ch)?

So that scanf would ignore all spaces before the first non-space character is encountered in the stream.

Also what is input buffer in fflush(stdin)?

What you input into the console will exist in the stdin stream.

Don't flush that stream however, it's undefined behavior. If you want to discard characters entered after scanf is called, you can read and discard them.

user123
  • 8,970
  • 2
  • 31
  • 52
  • Thanks everybody but even if I flush input stream,will it flush the whole input stream or just the white spaces or newline characters? Also When i would realize that i have to remove white spaces in an input stream of characters or i have to explicitly do that in every reading of characters? – Rahul Kathuria Jun 14 '13 at 15:59
2

The space in the scanf in this case tells scanf to ignore any leading whitespace characters in front of the character you read. Still even if there is no whitespace in front of the character the code will work and read the character successfully.

I am not sure what you are asking in your last question, but stdin is the standard input stream for you program.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • Thanks everybody but even if I flush input stream,will it flush the whole input stream or just the white spaces or newline characters? Also When i would realize that i have to remove white spaces in an input stream of characters or i have to explicitly do that in every reading of characters? – Rahul Kathuria Jun 14 '13 at 16:04
2
scanf(" %c",&ch);

As per the man page,

White space (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input.

Stdin is standard input.The user enters the data for the program, this is first stored in a buffer and then when the program requests data transfers by use of the read operation the data is made available to the program. (using scanf etc).

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
  • Thanks everybody but even if I flush input stream,will it flush the whole input stream or just the white spaces or newline characters? Also When i would realize that i have to remove white spaces in an input stream of characters or i have to explicitly do that in every reading of characters? – Rahul Kathuria Jun 14 '13 at 16:01
  • @RahulKathuria `fflush` will flush all the characters. [However it is not correct to use with with stdin](http://stackoverflow.com/questions/2979209/using-fflushstdin). – Suvarna Pattayil Jun 14 '13 at 17:45
0

I had the same problem a while ago in which if I would try to read a variable using scanf ("%c", &ans); it would not read anything. Thus I figured out that the \n character from the last input was being read.

Thus, doing scanf (" %c", &ans); solved my problem.

Although, I could not understand your second question clearly.

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
-1

Just to give a space from the last object, if not, for example a string, everything will be together with no spaces between them.

  • 2
    I think you need to re-read the question, your answer isn't correct and doesn't make sense. OP is talking about `scanf`, not `printf` – Mike Jun 14 '13 at 15:09
  • Thanks everybody but even if I flush input stream,will it flush the whole input stream or just the white spaces or newline characters? Also When i would realize that i have to remove white spaces in an input stream of characters or i have to explicitly do that in every reading of characters? – Rahul Kathuria Jun 14 '13 at 16:04