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 is 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 delimiter is less than the variable, the additional variable contains null.
Syntax
Sscanf (string, format, arg1, arg2, arg ++) parameter description
String is required. Specifies the string to be read.
Format is required. The format to be used.
Optional. The first variable that stores data.
Optional. The second variable that stores the data.
Optional. The third and fourth variables used to store data. And so on.
Description
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)
Example
<? Php Tutorial
$ String = "age: 30 weight: 60 kg ";
Sscanf ($ string, "age: % d weight: % dkg", $ age, $ weight );
// Show types and values
Var_dump ($ age, $ weight );
?> Output:
Int (30)
Int (60)
Scanf ("a = % f, B = % f, c = % f", & a, & B, & c );
The input format should be as follows:
A = 12, B = 24, c = 36
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.
-- Tan Haoqiang, C language programming (version 2nd), Tsinghua University Press, November 2008, p71 ~ 72
This article is very confusing. It seems like the beginning is true, but it is actually stupid.
Programs are used to solve problems for users, rather than to make users more difficult.
No qualified programmer can write
Scanf ("a = % f, B = % f, c = % f", & a, & B, & c );
Such code. The reason is that "a =", "B =", "c =", and "," are meaningless. It is not difficult to find out how stupid the code is. It is basically equivalent
Scanf ("a = ");
Scanf ("% f", & );
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 user input errors, the program may also be suspended or an error occurs.
So 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.
The citation should be modified
"If yes
Scanf ("a = % f, B = % f, c = % f", & a, & B, & c );
The input format should be 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 ."
This should be the case for user-friendly and easy-to-use program code.
Printf ("a = ");
Scanf ("% f", & );
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.