EOF symbolic constants in C Language

Source: Internet
Author: User

First readCode

Code

  /*  Copy input to output:

Read one character
While (this character is not a file end indicator)
Output the characters that have just been read
Read the next character
*/
# Include < Stdio. h >

Int Main ()
{
Int C;

While (C = Getchar ()) ! = EOF)
{
Putchar (C );
}
Return 0 ;
}

How can we distinguish between valid data and input terminator in a file?

The solution of C language is: when no input is made, the getchar function returns a special value, which is different from any actual character. This value is EOF (end of file, end of file ). When declaring the variable C, we must make it large enough to store any value returned by the getchar function. The reason why C is not declared as char is that it must be large enough to store the file Terminator EOF in addition to any possible characters.

The EOF input is locked by the system. CTRL + Z in windows and CTRL + D in Linux/Unix. For details, refer to S.Here.

 

 Print the EOF Value

   code highlighting produced by actipro codehighlighter (freeware) 
http://www.CodeHighlighter.com/
--> /* Print the EOF value
*/
# include stdio. h >
int main ()
{< br> printf ( " EOF is % d \ n " , EOF);
return 0 ;< BR >}

The symbol constant EOF is defined in the header file <stdio. h>.Program, The EOF outside the double quotation marks of the printf () statement will be replaced with the header file <stdio. h>.

# Define EOF

Defined value. The defined values are not unique in different systems. This is why the use of standard constants such as EOF can increase program portability.

 

 

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.