In the role of EOF while (scanf ( "% d", & n)) of

Reference from: https://blog.csdn.net/henu1710252658/article/details/83040281

In the C language, or more precisely to the C standard library representing EOF (end of file). In the while loop to EOF as the end of the file, such as files ending with an EOF mark, it must be a text file. In a text file, the data is stored in the form of ASCII code value of the character. We know, ASCII code values ​​range from 0 to 127, -1 impossible, can be used as end of file marker EOF used.

#include<stdio.h>

int main(){
    int a,b;
    while(scanf("%d %d",&a,&b)!=EOF){
        printf("%d",a+b);
    }
    
}

When the above program is running, if not "! = EOF", then this program is a loop, will run forever; plus the "! = EOF" This program is not a cycle of death, if not in the terminal the program will automatically enter the end (while meant to say when the current input buffer as well as what has been read, stop until the input is empty the contents of the cache) .
In this "scanf ("% d ", & n)! = EOF" is equivalent to "scanf ("% d ", & n)! = EOF", or "(" & n,% d ) "~ scanf", or "scanf ( "% d", & n ) == 1 ". Return Value scanf is determined by the following parameters
scanf ( "% d% d" , & a, & b);
if a and b have been successfully read, then scanf return value is 2; if only a read successfully into the return It is 1; if a and b are not successfully read, the return value is 0; if the error or encounters end of file, the EOF is returned, and the return value of type int.
However, this usage is not present in C ++, but have the same function while ((cin >> a) = 0!):
Not previously appreciated that while the inside with cin >> a; what is meant, CIN is the input stream C ++ objects, ">>" is overloaded operator, CIN >> The return value is cin object.
When using this condition, then, by detecting the state of the stream to determine the end; (1) If the flow is valid, i.e., the stream errors are not encountered, then the detection is successful;
(2) If they are encountered end of file, or a invalid input (for example, the input value of this title is not an integer), istream state of the object will become invalid, the condition is false; read failure when you can not continue to read, then read operation is ended, while ( cin >> a) returns false, out of the loop!
In C ++ while (cin >> n, n) :
His role is: to enter a number, this number is not zero into circulation, out of the loop is zero.
----------------

Guess you like

Origin www.cnblogs.com/Theo-sblogs/p/11448342.html