+ 3

Can you find any BUG in my code?

#include <stdio.h> int main() { char ch; while((ch=getchar)!=p) { putchar(ch); } return 0; }

21st Apr 2021, 4:08 PM
Дилшод Усмонов
Дилшод Усмонов - avatar
6 odpowiedzi
+ 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().
21st Apr 2021, 4:18 PM
Shadow
Shadow - avatar
+ 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.
21st Apr 2021, 4:45 PM
Shadow
Shadow - avatar
+ 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
21st Apr 2021, 5:19 PM
Shadow
Shadow - avatar
+ 3
Is -1 equil to EOF?
21st Apr 2021, 4:42 PM
Дилшод Усмонов
Дилшод Усмонов - avatar
+ 2
#include <stdio.h> int main() { char ch; while ((ch=getchar())!=-1) putchar(ch); return 0; } // Hope this helps
21st Apr 2021, 4:38 PM
Calvin Thomas
Calvin Thomas - avatar
+ 2
Shadow Thanks for that remark. So is it solely upto the compiler to define EOF?
21st Apr 2021, 4:58 PM
Calvin Thomas
Calvin Thomas - avatar