While (scanf ("% d", & N) of ACM )! = EOF)

Source: Internet
Author: User
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/

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.