I have this but once it reaches the supposed EOF it just repeats the loop and scanf again.
int main(void) { char words[16]; while(scanf("%15s", words) == 1) printf("%s\n", words); return 0; }
scanf returns EOF if end of file (or an input error) occurs before any values are stored. If any values are stored, it returns the number of items stored; that is, it returns the number of times a value is assigned by one of the scanf argument pointers.
In computing, end-of-file (EOF) is a condition in a computer operating system where no more data can be read from a data source.
Try:
while(scanf("%15s", words) != EOF)
You need to compare scanf
output with EOF
Since you are specifying a width of 15
in the format string, you'll read at most 15 char. So the words char array should be of size 16
( 15 +1
for null
char). So declare it as:
char words[16];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With