Definition of EOF and how to use it when tively)

Source: Internet
Author: User

Http://faq.cprogramming.com/cgi-bin/smartfaq.cgi? Answer = 1048865140 & id = 1043284351 & utm_source = newletter & utm_medium = email & utm_campaign = twentyeight-followup

Translated by Hammer and ljbsdu

The use and significance of EOF seems to have brought a lot of confusion to novice programmers. I hope this explanation will help you better understand it. Before explaining in depth what EOF is, let me tell you what EOF is.
EOF is not:
1. One character;
2. a value that exists at the end of the file;
3. a value that can exist in the middle of the file;
So what exactly is it?
EOF is an int type macro defined as a negative value. It is usually used as the return value of the read operation function, marking a read operation error or reaching the end of the input. Because of the variable escalation rules (discussed in detail later), you must remember to use int variables to save the return values of these (read) functions, even if the return values of the functions seem to be char, such as getchar () or fgetc ();

Below are some examples of code that you may use:
Int c; </p> <p> while (c = fgetc (fp ))! = EOF) <br/>{< br/> putchar (c); <br/>}</p> <p>

Int ch; </p> <p> while (ch = cin. get ())! = EOF) <br/>{< br/> cout <(char) ch; <br/>}< br/>
Improvement from char to int
According to the definition, int represents a value greater than char, so an int with a negative value cannot contain the same value as char (??). However, when you compare an int and char type, the char type will be upgraded to the int type to explain (?) Variable differences in the number of digits. The upgraded char value is affected by its symbol. Unfortunately, a char variable can be either signed or unsigned, depending on the compiler.
For better understanding, let's take a look at some representative numbers of int and char.

Suppose that int Is 2 bytes (your compiler may be more), and char only uses 1 byte (this is consistent with your compiler ). Except the first column, the values below are represented in hexadecimal notation.
 Convert ------------------------------ <br/> | char and int comparison | char to int promotion | <br/> convert ---------------------------- <br/> | Decimal | int | char | unsigned | signed | <br/> | --------- | ------- | ---------- | --------- | <br/> | 2 | 00 02 | 02 | 02 | 00 02 | 00 02 | <br/> | 1 | 00 01 | 01 | 01 | 00 01 | 00 01 | <br/> | 0 | 00 00 | 00 | 00 | 00 | 00 | 00 00 | <br/> |-1 | FF | 00 FF | <br/> |-2 | ff fe | FE | 00 FE | ff fe | <br/> ----------------------------- ------------------------------ <br/>

From the "char to int promotion" table, we can see that the char symbol leads to different int values.

So what does this mean for a programmer?
Well, let's take a look at the modified version of the above Code. This time we will mistakenly use a char variable to save the return value of the fgetc () function;

Char c; </p> <p> while (c = fgetc (fp ))! = EOF) <br/>{< br/> putchar (c); <br/>}</p> <p>

Now we assume that we are reading a byte in the file with a value of 0xff. Fgetc () will return this value in int type, so it looks like this 0x00 0xff (same

, I suppose an int is 2 bytes ). To store this value in char, char must be downgraded, so the byte value is changed to 0xff.
Then, compare byte c with int type EOF. The promotion rule takes effect. c must be upgraded to the int type. However, in the example code, the c symbol is not explicitly declared, so we do not know whether it is signed or unsigned, therefore, the upgraded int value may be 0xff 0xff or 0x00 0xff. Therefore, the Code does not necessarily run as required.
Below is a small program to help demonstrate this improvement.
 # Include <stdio. h> </p> <p> int main (void) <br/> {<br/> int I =-1; <br/> signed char SC = 0xff; <br/> unsigned char usc = 0xff; </p> <p> printf ("Comparing % x with % x/n", I, SC ); <br/> if (I = SC) puts ("I = SC"); <br/> else puts ("I! = SC "); <br/> putchar ('/N'); <br/> printf (" Comparing % x with % x/n ", I, usc ); <br/> if (I = usc) puts ("I = usc"); <br/> else puts ("I! = Usc "); </p> <p> return 0; <br/>}</p> <p>/* <br/> * Output </p> <p> Comparing ffff with ffff <--- Notice this has been promoted <br /> I = SC </p> <p> Comparing ffff with ff <br/> I! = Usc </p> <p> * <br/> */<br/> 
Another consideration is when char is unsigned. In this case, the degradation or escalation of the return value of the fgetc () function will impair the value of EOF and cause the program to fall into an endless loop. Let's take a look at the entire process:
----- Fgetc () returns EOF (0xff 0xff) at the end of the input, is downgraded to 0cff, and put it in an unsigned char variable c, the unsigned char c is upgraded to int, and the value changes from 0xff to 0xff 0xff. The EOF is compared with c, that is, the comparison between 0xff 0xff and 0x00 0xff. The result is false (because the values are different), which is not expected. Fgetc () is called again, and EOF is still returned. The endless loop starts!

The following code demonstrates this problem.
# Include <stdio. h> </p> <p> int main (void) <br/> {<br/> FILE * fp; <br/> unsigned char c; </p> <p> if (fp = fopen ("myfile.txt", "rb") = NULL) <br/>{< br/> perror ("myfile.txt"); <br/> return 0; <br/>}</p> <p> while (c = fgetc (fp ))! = EOF) <br/>{< br/> putchar (c); <br/>}</p> <p> fclose (fp ); <br/> return 0; <br/>}</p> <p>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.