The return value of the C language sacanf

Source: Internet
Author: User

1. The scanf function has a return value, and its return value can be divided into three different cases


1) A positive integer that indicates the number of correct input parameters. For example, execute scanf ("%d%d", &a, &b);
If the user input "3 4", you can enter correctly, return 2 (correctly entered two variables);
If the user enters "3,4", you can enter a correctly, you cannot enter B, and return 1 (a variable is entered correctly).


2) 0, indicating that the user's input does not match and that no value can be entered correctly. As the example above, if the user enters ", 3 4", returns 0.


3) EOF, which is a constant defined within the stdio.h (usually a value of-1), indicating that the input stream has ended. Under Windows, the user presses CTRL + Z (sees a ^z character) and then presses ENTER (which may need to be repeated 2 times), which means that the input ends; Linux/unix uses ctrl+d to indicate the end of the input.

So you can use the following code to handle the input:
While (scanf ("%s%c%c", str, &oldchar, &newchar) = = 3)/* or! = EOF , but the former is better */
{
; Processing
}

2. Why do I need spaces between%s and%c in the scanf format string above?
That's because if there's no space ... The Oldchar input is a space = =.

3. By the way, the return value of printf is the number of characters in the output, for example, the return value of printf ("1234") is 4, and the return value of printf ("1234\n") is 5.

The return value of the C language sacanf

Related Article

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.