An in depth reading of the symbolic constant EOF in C language

  • 2020-05-05 11:37:35
  • OfStack

EOF is the end of a file and is a macro definition
With the help of the getchar and putchar functions,
can be written without any other input/output knowledge An astonishing amount of useful code. The simplest example is copying the input one character at a time to the output, with the basic idea
As follows:

Read a character while (which is not the end of the file indicator) to print the character just read to the next character

Translate the above basic ideas into C language program:


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

         , the relationship operator! = means not equal to.
The           character is in bit-mode inside the machine, no matter what form it takes on the keyboard, on the screen, or anywhere else The store. The char type is specifically used to store this type of character data, although any integer (int) can also be used to store the word
Type data. We use the int type here for potentially important reasons.
Here we need to work out how to distinguish the valid data in the file from the input terminator. The solution for C is:
When there is no input, the getchar function returns a special value that is different from any actual character. The
The value is called EOF (end of file, end of file). When we declare the variable c, we have to make it big enough to store
Put any value returned by the getchar function. The reason c is not declared as char is that it must be large enough,
In addition to being able to store any possible characters, you also need to be able to store the file terminator EOF. Therefore, we declare c as int type.
EOF is defined in the header file < stdio.h > Is an integer, and it doesn't matter what the value is, as long as it's associated with
The values of any char type are different. Symbolic constants are used here to ensure that the program does not need to rely on its corresponding
Any particular number.
For experienced programmers of the C language, you can write a more concise copy of this character. In C
In language, this is similar to


c = getchar()

An assignment like this is an expression and has a value that the left variable holds after the assignment. In other words,
Assignments can appear as part of a larger expression. If you place the c assignment on the while test
of the while loop statement In the test section, the character copying program can be rewritten as


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

In this program, the while loop first reads a character and assigns it to c, then tests to see if the character is a file
End sign. If the character is not the end of the file flag, the while body is executed and the character is printed. Then repeat
Execute the while statement. When the end of the input is reached, the while loop terminates execution, thus the entire main function
Number execution ends.
          and above this procedure centralizes the input. The getchar function appears only once in the procedure, which shortens the procedure,
The whole process looks more compact. Once you get used to this style, you'll find that programs written this way are easier to read.
We often see this style. (however, if we use this type of complex statement too much, we can write a program with
Can be difficult to understand, should try to avoid this situation.
For the conditional part of the while statement, the parentheses around the assignment expression cannot be omitted. Not equal to operator! = the
The precedence is higher than that of the assignment operator =, so that the relationship is tested without parentheses! = will be assigned to =
Execute before operation. So statement


c = getchar() != EOF

Equivalent to statement


c = (getchar() != EOF)

validation and printing EOF

1. Verify the expression getchar()! Is equal to EOF 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 characters, and getchar() does not return a file terminator (EOF) when there are characters to read, so getchar()! The value of = EOF is true, and the variable c will be assigned to 1. When the program encounters a file terminator, the expression value is false, the variable will be assigned a value of 0, and the program will end.

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

2. Please write a program
to print EOF values


#include <stdio.h>

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

The symbolic constant EOF is defined in the header file stdio.h, in which the EOF in the printf() statement with the double quotes 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 some other value. This is why using standard symbolic constants such as EOF increases program portability.


Related articles: