+ 3
Can you find any BUG in my code?
#include <stdio.h> int main() { char ch; while((ch=getchar)!=p) { putchar(ch); } return 0; }
6 Answers
+ 6
You are missing parentheses when calling getchar(), and the name 'p' is undefined. You can use the EOF macro to check the success of getchar().
+ 4
EOF must expand to a negative integer constant, but the exact value is implementation defined, although commonly -1 is chosen. To be on the safe side, I would prefer using EOF over -1 directly.
+ 4
Whatever is defined for the macro in the implementation of the standard library that your compiler uses, yes. The C standard only says it should be a negative integer constant, a quoted excerpt can be found here:
https://stackoverflow.com/questions/4705968/what-is-value-of-eof-and-0-in-c
+ 3
Is -1 equil to EOF?
+ 2
#include <stdio.h>
int main() {
char ch;
while ((ch=getchar())!=-1) putchar(ch);
return 0;
}
// Hope this helps
+ 2
Shadow Thanks for that remark. So is it solely upto the compiler to define EOF?