The following code in C accepts integers as long as data is fed into it.
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
while(scanf("%d",&x)!=EOF)
{
printf("%d\n",x);
}
getche ();
}
It works fine for integer inputs but on entering any character/float input it becomes an infinite loop.Why is it so ?
If scanf
failes to parse any input, it will leave the input in the buffer and return 0
. As the erroneous input will never leave the buffer and you don't check for anything but EOF
being returned, the loop will continue for ever.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments