I know it might sound stupid but how can I exit this loop ?
#include <stdio.h>
#include <stdlib.h>
int main(){
char c;
do {
c = getchar();
printf("%c", c);
}while(c != EOF);
return 0;
}
I'm reading a book and trying to do the following exercise: "Verify that the expression getchar() != EOF is 0 or 1" If I read the value of EOF stored in an integer value it will be equal to -1 but if I try to catch a -1 as a char is mindfuck. From what I've understood EOF is a value which is not assigned to any other char ..
anyone can help ?
edit1: I know that c should be an Integer ... I'm reading it as a char intentionally.
edit2:
int main(){
int c;
while((c = getchar()) != EOF)
{
printf("%d\n", c);
}
return 0;
}
----->
int main(){
int c;
int i = 0;
char str[2];
while((c = getchar()) != EOF)
{
str[i] = c;
++i;
if(i > 1) i = 0;
if(str[0]=='-'&&str[1]=='1')
{
c = EOF; // doens't exit loop
}
else printf("%d\n", c);
}
return 0;
}
Why I can't understand this.
