I am traversing hex input from stdin in my code, and I noticed that I can't get around my code stopping when a 0xff byte is reached. I know that this happens because loop is :
while( (c=getchar()) != EOF)
However, I can't leave this out because otherwise getchar is called after the real End-Of-File and it segfaults. How can I traverse the full input correctly?
(sorry I know that there should be a simple answer to this seemingly common task but I tried searching for some time and found nothing)
You have declared c
as a char, but getchar()
returns an int
. The correct loop construct for C is this:
int c;
while ( (c = getchar()) != EOF ) {
// use c here.
}