I'm new in the journey of learning C and to my knowledge, I knew that while loop would run forever if someone gives condition such as while (1 == 1)
it would become a forever loop.
But While reading from a book I noticed a code such as
#include<stdio.h>
int main() {
char a;
int started = 0;
puts("Scanning\n");
while (scanf("%s[^\n]",&a) == 1)
{
if (a >= 65 && a <= 90)
{
printf("Char is capital\n");
}
else
{
printf("Not cap.\n");
}
}
puts("Done\n");
return 0;
}
Command line => char.exe < file.txt
It takes input from stdin but scanf returns the number of argument i.e 1 so the condition should become while (1 == 1)
But why this is not a forever loop and exists after reading the file?
Thanks
Copyright Notice:Content Author:「Rudalf」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/56581647/not-sure-why-while-loop-works-different-when-using-scanf