You are on page 1of 3

The use and meaning of EOF seems to cause a lot of confusion with some new coder s, hopefully this

explanation will help you understand better. Before I go into too much detail about what EOF is, I'll tell you what it isn't. EOF is NOT: A char A value that exists at the end of a file A value that could exist in the middle of a file And now to what it actually is. EOF is a macro defined as an int with a negative value. It is normally returned by functions that perform read operations to denote either an error or end of in put. Due to variable promotion rules (discussed in detail later), it is importan t to ensure you use an int to store the return code from these functions, even i f the function appears to be returning a char, such as getchar() or fgetc(). Here are some code examples that you might use:

int c; while ((c = fgetc(fp)) != EOF) { putchar (c); }

int ch; while ((ch = cin.get()) != EOF) { cout <<(char)ch; }

char to int Promotion By definition an int is larger than a char, therefore a negative valued int can never hold the same value as a char. However, when you compare an int with a cha r, the char will get promoted to an int to account for the difference in size of the variables. The value of a promoted char is affected by its sign, and unfort unately, a char can be either signed or unsigned by default, this is compiler de pendant. To understand this better, let's look at the representation of a few numbers in both ints and chars. The following assumes 2 byte ints (your compiler might use a larger amount). A c har uses only 1 byte (this will be the same amount on your compiler). With the e xception of the first column, the values are shown in hexadecimal.

usc"). sc"). so it looks like this: 0x00 0xff (again.------2 00 02 02 1 00 01 01 0 00 00 00 -1 FF FF FF -2 FF FE FE ----------------------------- -----------------------------char to int promotion -----------------------------char unsigned signed ------. Promotion rules apply. Next. fgetc() returns this value within an int. To store this value in a char. i.---------.--------02 00 02 00 02 01 00 01 00 01 00 00 00 00 00 FF 00 FF FF FF FE 00 FE FF FE ------------------------------ The "char to int promotion" table makes it clear that the sign of a char produce s a very different number in the int. this time incorrectly using a char variable to store the return code from fgetc(). char c. it must be dem oted. The following is a short program to help show the promotion: #include <stdio.----------------------------char and int comparison ----------------------------Decimal int char --------. while ((c = fgetc(fp)) != EOF) { putchar (c). and the char value becomes 0xff. so the int value coul d become either 0xff 0xff or 0x00 0xff.--------. printf ("Comparing %x with if (i == sc) puts("i == else puts("i != putchar ('\n'). usc"). %x\n". the sign of c isn't explici tly declared. printf ("Comparing %x with if (i == usc) puts("i == else puts("i != %x\n". } Now let's assume that within the file we are reading from is a byte with value 0 xff.h> int main(void) { int i = -1. the char c is compared with the int EOF. sc). However. so we don't know if it's signed or unsigned. usc). So what does all this mean to me as a programmer? Well. let's have a look at a revised version of the code shown above. i. . sc"). signed char sc = 0xff. the code is is not guaranteed to work in the way we require. unsigned char usc = 0xff. I'm assuming 2 byte ints). and c must be promoted to an int. in the sample code. Therefore.

"rb")) == NULL) { perror ("myfile. unsigned char c. } while ((c = fgetc(fp)) != EOF) { putchar (c). return 0. The endless loop begins. The result is FALSE (the values are different). } . the pr ocess of demoting and promoting the returned value from fgetc() will have the af fect of corrupting the EOF value. meaning comparison is between 0xff 0xff and 0x00 0xff.h> int main(void) { FILE *fp. and the program will get stuck in a infinite l oop.Notice this has been promoted - EOF (0xff 0xff) is returned by fgetc() due to end of input Value demoted to 0xff to be stored in unsigned char c unsigned char c promoted to an int. The following code demonstrates this problem.return 0. return 0. and still returns EOF. } fclose(fp). which is undesirable. if ((fp = fopen("myfile. } /* * Output Comparing ffff with ffff i == sc Comparing ffff with ff i != usc * */ Another scenario to consider is where the char is unsigned. #include <stdio. In this case. value goes from 0xff to 0x00 0xff EOF is compared with c.txt"). Let's follow that process through: <--. fgetc() is called again.txt".