Halo,
I was just going through "C programming gotchas" over the internet. There I found this issue. For the code below:
This is what they've qoutedCode:#include<stdio.h> int main(void) { char ch; ch=getchar(); while(ch!=EOF) { putchar(ch); ch=getchar(); } }
If I take ch to be of unsigned type then it's fine that the loop will never terminate coz ch will always be positive. But what if I keep it signed as shown above then is it ever possible that ch will be 255(for which ch will be interepreted as -1)? I mean except for Cntrl+Z nothing will break the loop ? Then why it's converted to "int ch"? Why not it remain as "char ch"?The loop may never terminate: if char is an unsigned type then EOF will be converted to some positive value. On systems with where char is signed, there is a more subtle bug. Suppose for example that EOF is -1 - then if character 255 is read it will be converted to the value -1 and terminate the input prematurely.
Thanks