when the following code is compiled it goes into an infinite loop:
int main()
{
unsigned char ch;
FILE *fp;
fp = fopen("abc","r");
if(fp==NULL)
{
printf("Unable to Open");
exit(1);
}
while((ch = fgetc(fp))!=EOF)
printf("%c",ch);
fclose(fp);
printf("\n",ch);
return 0;
}
The gcc Compiler also gives warning on compilation
abc.c:13:warning: comparison is always true due to limited range of data type
the code runs fine when unsigned char
is replaced by char
or int
as expected i.e. it terminates.
But the code also runs fine for unsigned int
as well.
as i have i have read in EOF
is defines as -1
in stdio.h
then why does this code fails for unsigned char but runs fine for unsigned int.