-1
void main()
{
   float x;
   while(scanf("%f",&x) != 0)
   printf("%f\n",x); 
}

The above code takes input from stdin and keeps repeating it but how to end this? I know scanf can return EOF so if I add a check like

while(scanf("%f",&x) != EOF)

Which input from stdin can cause any of the above two condition to fail? ctrl+d will make the program end but I want to know is there any specific input which can make this condition fail?

Gopi
  • 19,784
  • 4
  • 24
  • 36
  • Provide a file to read from, once it reaches the end of the file the loop will terminate. Remember to use input redirection – smac89 Oct 10 '14 at 06:06
  • Yeah reading from a file will return EOF. But how to make this condition fail by giving input fron stdin? That is my question – Gopi Oct 10 '14 at 06:07
  • press ctrl + D (this will make both fail) on your keyboard or enter a string instead (this will make first one fail) – smac89 Oct 10 '14 at 06:08
  • I think you have to check return value of `scanf`. In your case only `%f` and will return exactly `1`. So you can check by `scanf("%f", &x) == 1`. – Jayesh Bhoi Oct 10 '14 at 06:09

3 Answers3

1

scanf function returns the number of input items successfully scanned.

The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs.

In your program always it return 1. To prove see the code..

        #include<stdio.h>

          main()
          {
            int a;float x;
            while((a=scanf("%f",&x)) != 0)
            printf("%f %d\n",x,a);
          }

In this program a is always 1. Be cause only one value is scanned successfully. a gets 2 for , if you scan two values.

Anbu.Sankar
  • 1,326
  • 8
  • 15
  • I know the above code will always return 1. I was curious to know is there any other way to make this condition fail. So the answer to it is NO right? – Gopi Oct 10 '14 at 06:15
  • Give input as any character. It will be terminate. Because When successfully scanned , that time only it returns one. while getting character (for example) it will fails. – Anbu.Sankar Oct 10 '14 at 06:19
0

Looking at linux manual pages:

scanf: These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.

So, using your original program:

void main()
{
   float x;
   while(scanf("%f",&x) != 0)
   printf("%f\n",x); 
}

That means that if you enter anything that is not convertible, will end your program. Try it and enter any letter and hit enter

Amadeus
  • 10,199
  • 3
  • 25
  • 31
  • I understand that conversion failure will return 0. So for the user we need to explicitly mention to enter some character after entering valid values right? Just a thought!! Thanks!! – Gopi Oct 10 '14 at 06:22
0

Ctrl + D or Ctrl + Z,

Similar questions already asked before:

End of File(EOF) of Standard input stream (stdin)

Community
  • 1
  • 1
Kaizhe Huang
  • 990
  • 5
  • 11