Sscanf definition and usage and analysis

Source: Internet
Author: User
Sscanf definition, usage, and analysis: the sscanf () function parses input from a string based on the specified format. if only two parameters are passed to the function, the data will be returned in the form of an array. Otherwise, if additional parameters are passed, the parsed data will be stored... Sscanf definition and usage and analysis:The sscanf () function parses input from a string according to the specified format.

If only two parameters are passed to the function, the data will be returned as an array. Otherwise, if additional parameters are passed, the parsed data will be stored in these parameters, if the number of delimiters is greater than the number of variables that contain them, an error occurs. However, if the number of delimiters is less than the variable, the additional variable contains null.

Syntax:Sscanf (string, format, arg1, arg2, arg ++)

Parameter description

String required, specifying the string to be read.

Format is required, which specifies the format to be used.

Optional. arg1 is the first variable for storing data.

Optional. arg2 is the second variable used to store data.

Arg ++ (optional) The third and fourth variables used to store data.

Note:The format parameter is the conversion format. it starts with the percent sign ("%") and ends with the conversion character. the following possible format values:

%-Percentage sign returned

% B-binary number

% C-characters based on ascii values

% D-signed decimal number

% E-resumable counting (for example, 1.5e + 3)

% U-unsigned decimal number

% F-floating point number (local settings aware)

% F-floating point number (not local settings aware)

% O-octal values

% S-string

% X-hexadecimal (lowercase letter)

% X-hexadecimal (uppercase letters)

The PHP instance code is as follows:

 

This form is used to allow users to add necessary information when entering data, so that the meaning is clear and it is not easy to make input data errors. programs are used to solve problems for users, it is not used to add trouble to users.

No qualified programmer can write: scanf ("a = % f, B = % f, c = % f", & a, & B, & c ); the reason for this code is that "a =", "B =", "c =", and "two", "are meaningless, it is not difficult to find out that this lame code is slightly changed. it is basically equivalent:

scanf("a="); scanf("%f",&a); scanf(","); scanf("b="); scanf("%f",&b); scanf(","); scanf("c="); scanf("%f",&c);

Among them, scanf ("a =");, scanf ("B =");, scanf ("c ="); and scanf (","); there is no information value at all for the program. The only effect of adding the code is to add unnecessary troubles to the user. in case of input errors, it can also cause program "failure" or errors.

Therefore, the citation is not only stupid, but it is simply instigating the learner to abuse themselves-the programmer is always the first user of the program.

That citation should be changed to "if it is: scanf (" a = % f, B = % f, c = % f ", & a, & B, & c );

The input format is as follows: a = 12, B = 24, c = 36

This form is used to make it difficult to add unnecessary troubles to the user when entering data, confusing the meaning, and prone to input data errors ", the following code should be written for user-friendly and easy-to-use programs:

printf("a="); scanf("%f",&a); printf ("b="); scanf("%f",&b); printf ("c="); scanf("%f",&c); 

Call the printf () function to give a prompt to the user, instead of using code like "scanf (" a = ");" to torture the user.

Address:

Reprinted at will, but please attach the article address :-)

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.