Return values of scanf (), printf (), fscanf (), and fprintf ()

Source: Internet
Author: User

The return values of scanf (), printf (), fscan (), and fprintf () are int type.

 

The return value of scanf () is the number of successfully entered data.

For example, scanf ("% d % s", & A, & B, S); 3 is returned if execution is successful.

Scanf ("% d", & A, & B); returns 2 if execution is successful.

If scanf ("% d", & A, & B) in the input process; for some reason, if A is successful, 1 is returned, if a and B fail, 0 is returned.

If an error occurs or an end of file is encountered, EOF is returned (generally, the macro defines EOF as-1 ).

 

 

Printf () returns the number of successfully output characters. Both int and escape characters are output by character.

For example, int A = 1, B = 2;

Char s [] = "hello ";

Int J;

J = printf ("% d % s", A, B, S );

Printf ("% d \ n", J );

Output: 12 hello

8

Why is it 8 instead of 7? The end escape character '\ 0' is at the end of string S'

If the preceding j = printf ("% d % s", a, B, s) is changed to j = printf ("% d % s",, b, s );

The output is: 1 2 hello // number of characters in the output sequence. Do not leave spaces empty.

10 // space is added between abs during output.

 

If it is changed to: int a = 10, B = 20;

Char s [] = "hello ";

Int j;

J = printf ("% d % s", a, B, s );

Printf ("% d \ n", j );

The output is: 1020 hello

10 // because the numbers 10 and 20 are two digits, each of them occupies two places in the output character sequence.

 

What is the result of printf failure? I really don't know. It seems that it has never failed.

 

Verify that the returned value of fscanf () is similar to that of scanf.

The returned value of fprintf () is similar to that of printf.

 

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.