ACM diary-and while (scanf ("% d", & N )! = EOF)"The test input contains several test instances. When N is 0, the input ends and the instance is not processed ."
This is the first time that I have never seen this kind of input requirement for the first question of ACM, I got stuck on the Internet. When I saw other people's code, there was a while (scanf (" % d ", & N )! = EOF)
"The scanf function can still be put in the while... What is EOF ..."
No one can ask for answers online...
Some people say that when EOF is equal to-1, it is actually the return value of the scanf function is not equal to-1, and the loop continues.
Some people say that EOF does not need to be used to process the end of the file.
....
I finally figured it out many days later.
The return value of scanf is determined by the following parameters.
Scanf ("% d", & A, & B );
If both A and B are read successfully, the return value of scanf is 2.
If only a is successfully read, the returned value is 1.
If neither a nor B is read, the return value is 0.
If an error occurs or an end of file is encountered, the returned value is EOF.
The return value is int type.
Verification:
Sign = scanf ("% d", & A, & B );
Printf ("% d \ n", a, B );
Printf ("% d \ n", sign );
However, when "a x" is input, the output sign is 0.
When Will EOF be output? In stdio. H, the macro is defined as-1.
According to the instructions, the scanf function can return EOF only when the first parameter is null (NULL pointer). Otherwise, returns the number of successfully formatted and assigned parameters (> = 0 ).
End of file, which is generally abbreviated as EOF in computer terms. In the operating system, no more data can be read from the data source.
At that time, this program was written as follows:
While (scanf ("% d", & N )! = EOF)
{
If (n = 0)
{
Break;
}
}
Finally, I gave the question ......
Later, let's look at other people's code and write it again.
While (scanf ("% d", & N) & n! = 0)
Later
Whlie (scanf ("% d", & N)
Later
While (scanf ("% d", & N), n)
......
Maybe it's the easiest to use the scanf function in C language.
From: http://raines8231.blog.163.com/blog/static/13623310720122269316911/