A deep understanding of symbolic constants in C language Eof_c language

Source: Internet
Author: User

EOF refers to the terminator of a file, which is a macro definition
With the help of GetChar and Putchar functions, you can write them without knowing other input/output knowledge
An astonishing amount of useful code. The simplest example is to copy the input one word at a time to the output, the basic idea of the Fu
As follows:

    • Read a character
    • While (this character is not a file end indicator)
    • Output the characters you just read
    • Read the next character

Translate the above basic idea into the C language program as follows:

#include <stdio.h>
/* Copy input to output 1st version *
/main ()
{
int c;
c = GetChar ();
while (c!= EOF) {
Putchar (c);
c = GetChar ();
}
}

Where the relational operator!= represents "not equal to".
Characters in the keyboard, screen or any other place in whatever form, it is in the machine is in bit mode
Stored. The char type is specifically used to store this type of character data, and of course any integer (int) can also be used to store words
Character data. For some potentially important reason, we use the int type here.
Here's how to distinguish between valid data in a file and an input terminator. The solution to the C language is:
In the absence of input, the GetChar function returns a special value that is different from any actual character. This one
The value is called EOF (end of file, ending). When we declare variable C, we have to make it large enough to exist.
Put any value returned by the GetChar function. The reason for not declaring C as a char type is because it must be large enough to
In addition to storing any possible characters, you can store the file Terminator eof. Therefore, we declare C to be of type int.
EOF is defined in header file <stdio.h>, is an integer, and its specific value is not important, as long as it
The values for any char type are different. Using symbolic constants here, you can ensure that your program does not depend on its corresponding
Any particular value.
For the more experienced C language programmers, you can write this character copy program more refined. In C
Language, similar to the

c = GetChar ()

The assignment operation is an expression that has a value, that is, the value that the left variable holds after the assignment. Other words
The assignment can appear as part of a larger expression. If you place the operation for C assignment in the while Loop statement
In the trial section, the above character replicator can be written in the following form:

#include <stdio.h>
/* Copy input to output 2nd version *
/main ()
{
int c;
while ((c = GetChar ())!= EOF)
Putchar (c);
}

In the program, the while Loop statement first reads a character and assigns it to C, and then tests whether the character is a file
End flag. If the character is not a file end flag, the while statement body is executed and the character is printed. Then repeat
Executes the while statement. When the end of the input is reached, the while Loop statement terminates execution, thus the entire main
Number execution ended.
The above program will be input centralized, the GetChar function only appears once in the program, thus shortening the program,
The whole program looks more compact. Accustomed to this style, the reader will find that the program written in this way is easier to read.
We often see this style. (However, if we use this type of complex statement too much, the program that you write can
Can be difficult to understand and should try to avoid this situation. )
The parentheses on either side of an assignment expression cannot be omitted from the conditional part of the while statement. Not equal to operator!=
Precedence is higher than assignment operator =, so the relationship test!= will be assigned when the parentheses are not used
Execution before the operation. So the statement

c = GetChar ()!= EOF

Equivalent to statement

c = (GetChar ()!= EOF)

Validate and print EOF

1. Verify that the value of the expression GetChar ()!= EOF is 0 or 1.

#include <stdio.h>

Main ()
{
 int c;
  
  while (c = (GetChar ()!= EOF)
   printf ("%d\n", c);
  printf ("%d-at eof\n", c);
}

The program reads the characters, and when a character is readable, getchar () does not return the file Terminator (EOF), so the GetChar ()!= EOF is true, and the variable C is assigned a value of 1. When the program encounters a file terminator, the expression takes a false value, at which point the variable is assigned a value of 0, and the program runs the end.

For a judgment expression, its return value would be a Boolean value.

2. Please write a program that prints the EOF value

#include <stdio.h>

Main ()
{
 printf ("EOF is%d\n", EOF);
}

The symbolic constant EOF is defined in the header file Stdio.h, in which EOF for the double citation in the printf () statement is replaced with the text immediately following #define EOF in the header file stdio.h.

In our system, EOF is defined as-1, but in other systems, EOF may be defined as another value. This is why using standard symbolic 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.