fgetc

Comparing unsigned char and EOF

末鹿安然 提交于 2019-11-27 02:01:50
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

fgetc, checking EOF

末鹿安然 提交于 2019-11-26 22:54:12
In the book Linux System Programming I have read some like this: fgetc returns the character read as an unsigned char cast to an int or EOF on end of file or error. A common error using fgetc is: char c; if ((c = fgetc()) != EOF) {...} The right version of this code is: int c; if ((c = fgetc()) != EOF) { printf("%c", (char)c); ... } So, why can't I cast a return value to char before comparing with EOF ? Why do I have to compare EOF exactly with int ? As EOF defined as -1 , isn't it normally casted to char ? Are there platforms/compilers where it is not true? Cacho Santa You can't cast the

fgetc does not identify EOF [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 17:18:40
问题 This question already has answers here : Difference between int and char in getchar/fgetc and putchar/fputc? (2 answers) Closed last year . The program below runs fine on various Solaris/Linux flavours, but not on AIX. However, if I replace while(c!=EOF) with while(c!=0xff) on AIX it runs completely fine. Any thoughts? I checked the fgetc man page on AIX, and it should return the EOF constant! #include <stdio.h> #include<unistd.h> #include <string.h> int main() { char c; FILE *fp; fp = fopen(

Comparing unsigned char and EOF

眉间皱痕 提交于 2019-11-26 09:52:26
问题 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

fgetc, checking EOF

僤鯓⒐⒋嵵緔 提交于 2019-11-26 08:28:42
问题 In the book Linux System Programming I have read some like this: fgetc returns the character read as an unsigned char cast to an int or EOF on end of file or error. A common error using fgetc is: char c; if ((c = fgetc()) != EOF) {...} The right version of this code is: int c; if ((c = fgetc()) != EOF) { printf(\"%c\", (char)c); ... } So, why can\'t I cast a return value to char before comparing with EOF ? Why do I have to compare EOF exactly with int ? As EOF defined as -1 , isn\'t it