#include <stdio.h>
int main()
{
int a[50],i,n=0;
while(scanf("%d",&a[n++])!=EOF);
n--;
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}
Let,
Input: 5 6 7 8 9
Output: 5 6 7 8 9
My question is why the above code works in Online C Compiler and gives proper EOF(-1)
But while running the code in offline C IDE like Codeblocks, it results in an infinite loop of input as it is not giving any EOF, and what to do to get a similar result in an offline compiler?
while(scanf("%d",&a[n++])!=EOF);you can dowhile(scanf("%d",&a[n++])==1);. Then the loop will stop at the first non-integer input.nis always less than 50while(n < 50 && scanf("%d",&a[n++])==1);1fromscanfis that if you enter data it cannot convert you'll go into an infinite loop, since0 != EOFtoo.