-3

I've just started to learn c programming. I don't know weather this question is silly or not. Where do i use

while(getchar()! ='\n') ;

When using scanf function in some program the above mentioned while loop is used whereas some other program doesn't use the while loop after the scanf function.

So, where should i use this while loop and where not to?

overmass123
  • 21
  • 1
  • 4
  • Simply said You SHOULD NOT use this at all unless there is special requirement. – Vedant Terkar Dec 22 '15 at 03:44
  • 2
    Well, what does that loop do? If you know what it does, you should be able to reason about when to use it either to answer this question for yourself or to state your question better ("I know that… but don't understand…"). If you don't know what it does, then you need to figure that out first, starting with experimentation, and then with asking questions about that. – Jon Hanna Dec 22 '15 at 03:46
  • 2
    @VedantTerkar : The special requirements are *exactly* what the question is about. Your point could apply to any code - you should not use any code that does not do what you need it to do - that is rather a platitude. – Clifford Dec 22 '15 at 04:40

3 Answers3

1

Use the loop to clean up the rest of a line after an error

You use code similar to the loop in the question when you think there is debris left on a line of input after you (attempted to) read some data from standard input but the operation failed.

For example, if you have:

int n;

if (scanf("%d", &n) != 1)
    …recover from error here?…

The error recovery should check for EOF (it is correct to use feof() here — but see while (!feof(file)) is always wrong for how not to use feof()), and if you didn't get EOF, then you probably got a letter or punctuation character instead of a digit, and the loop might be appropriate.

Note that you should always check that the scanf() operations — or any other input operation — succeeded, and the correct check for the scanf() family of functions is to ensure you got the expected number of successful conversions. It is not correct to check whether scanf() returned EOF; you can can get 0 returned when there's a letter in the input queue and you ask for a number.

Beware EOF

Incidentally, the loop in the question is not safe. It should be:

int c;
while ((c = getchar()) != EOF && c != '\n')
    ;

If the loop in the question gets EOF, it continues to get EOF, and isn't going to do anything useful. Always remember to deal with it. Note the use of int c; too — getchar() returns an int and not a char, despite its name. It has to return an int since it returns every valid character code and also returns a distinct value EOF, and you can't fit 257 values into an 8-bit quantity.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

While loop syntax :

while (condition test)
{
      // C- statements, which requires repetition.
      // Increment (++) or Decrement (--) Operation.
}

while(getchar()! ='\n') ;

Here your condition is getchar()!='\n'.

First getchar() will execute . It will ask input from user. This function returns the character read as an unsigned char cast to an int or EOF on end of file or error. While loop keep continue executing until return value will not match with '\n'

scanf() example : scanf("%s", &str1);

With scanf you can ask user for enter input one time only For more than one input you have to place scanf in loop.

Where as you ask to enter input to user in while loop condition So It will keep asking input from user until EOF signal occur.

Punit Vara
  • 3,744
  • 1
  • 16
  • 30
0

When the scanf() format string does not cause it to read the newline from the input buffer.

Most simple format specifiers will not read the newline. However if you use %c, any character may be read, including newline, in which case the loop will need modification:

while( ch != '\n' && getchar() != '\n' ) ;

Where ch is the variable receiving the %c input.

Because console input is normally line-buffered, failing to consume the newline will cause subsequent stdin reading functions to return immediately without waiting for further input, which is seldom what is intended.

You would not use such a loop in cases where multiple fields are entered in a single line but processed in separate scanf() (or other stdin function) calls.

Clifford
  • 88,407
  • 13
  • 85
  • 165