How to exit a while-loop?
#include <stdio.h> main(void) { char ch; while (1) { if ((ch = getchar()) != EOF) { break; } putchar(ch); } return 0; } How do I escape from this while ? I had tried with EOF but it didn't work. I think you mean: int ch; Because EOF won't fit in a char . Also: if ((ch=getchar()) == EOF) break; Your logic is backwards. This: char ch; is wrong, EOF doesn't fit in a char . The type of getchar() 's return value is int so this code should be: int ch; Also, as pointed out, your logic is backwards. It loop while ch is not EOF , so you can just put it in the while : while((ch = getchar()) != EOF)