Method 1:
- char szstring [] = "-2876.99812376443 " ;
- double db1;
- db1 = atof (szstring);
- printf ( "atof result: \ n " );
- printf ( "% F %. 12f %. 2f % E \ n " , db1, db1, db1, db1, db1);
- printf ( "%. 1e %. 1e %. 18e %. 18e \ n " , db1, db1, db1, db1);
Method 2:
-
- CharSzstring2 [] ="-2876.99812376443";
-
- DoubleDB2;
-
- Sscanf (szstring2,"% Lf", & DB2 );
-
- Printf ("\ Nsscanf result: \ n");
-
- Printf ("% F %. 12f %. 2f % E \ n", DB2, DB2, DB2, DB2, DB2 );
- Printf ("%. 1E %. 1E %. 18e %. 18e \ n", DB2, DB2, DB2, DB2 );
The two methods can get the same output! The output is as follows:
Atof result:
-2876.998124-2876.998123764430-2877.00-2.876998e + 003-2.876998e + 003
-2.9e + 003-2.9e + 003-2.876998123764108100e + 003-2.876998123764108100e + 003
Sscanf result:
-2876.998124-2876.998123764430-2877.00-2.876998e + 003-2.876998e + 003
-2.9e + 003-2.9e + 003-2.876998123764108100e + 003-2.876998123764108100e + 003
Many people do not know much about the functions of the sscanf family. I want to write down the specific usage of the functions of the sscanf family. I hope you can make common progress. If there is anything wrong, please feel free to raise them. First, let's look at some function prototypes of this family:
// Input data from the keyboard to the variable
- IntScanf (Char* Format ,...);
// Input data from string to variable, which is the same
- IntSscanf (Const Char* Buffer,Const Char* Format ,...);
-
- Int_ Sscanf_l (Const Char* Buffer,Const Char* Format, locale_t locale ,...);
- IntSwscanf (Const Wchar_t* Buffer,Const Wchar_t* Format ,...);
-
- Int_ Swscanf_l (Const Wchar_t* Buffer,Const Wchar_t* Format, locale_t locale ,...);
Next we will only look at the standard form of sscanf functions:
- IntSscanf (Const Char* Buffer,Const Char* Format ,...);
Let's talk about its return value. library functions almost all return values. Some may be very strange. How do few people use sscanf's return values? Sscanf returns the number of successfully received variables. For example, sscanf ("3.14159", "% F", & PI); the return value is 1.
The test is as follows:Program:
- # include
-
- int main ()
- {
- int A;
- printf ( "% d" , scanf ( "% d \ n" , & ));
- return 0;
- }
-
If you press enter at the beginning, the program will continue waiting for you to enter, because scanf will skip the blank characters when entering numbers. In the C programming language, scanf actually uses getchar () to accept a string consisting of digits and convert it to a number. If I enter Ctrl-z (CTRL-D on UNIX),-1 is returned (depending on the compiler ). this is actually the value of the constant EOF, that is, the returned EOF. If you do not enter a number, the return value is 0. But what if I enter a floating point number? In the example above, 1 will be returned, but the buffer will leave garbage. If it is scanf ("% d", & A, & B);, an error will occur. This can be done by using a library function fflush (stdin) to clear the buffer. However, it seems that this usage is not standard in the rain. K & R, but the behavior is not defined, but we can use while (C = getchar ())! = '\ N' & C! = EOF); it can also clear the following junk scanf format matching is relatively simple, it must be remembered that the common variables must be added, otherwise the compiler cannot detect errors, however, the running process is certainly a segment error.
-
- When there are too many other users, there are too many other users, too many other users.
-
- Code │ ┃
- ┠ ── ─ ┼ ── ─
-
- Percent % A │ read floating point value (c99 only) percent
-
- Percent % A │ read floating point value (c99 only) percent
- Bytes % C │ read single character bytes
-
- Percent % d │ read decimal integer percent
- Percent % I │ read decimal, octal, and hexadecimal integer percent
-
- Percent % E │ read floating point number percent
- Percent % E │ read floating point number percent
-
- Percent % F │ read floating point number percent
- Percent % F │ read floating point number (c99 only) percent
-
- Limit % G │ read Floating Point Number Limit
- Limit % G │ read Floating Point Number Limit
-
- Bytes % O │ read eight bytes
- Bytes % s │ read string bytes
-
- Percent % x │ read hexadecimal number percent
- Percent % x │ read hexadecimal number percent
-
- Limit % P │ read pointer value limit
- Characters % N │ the number of equivalent characters that have been read so far
-
- Percent % u │ read unsigned decimal integer percent
- Combined % [] │ scan Character Set
-
- Percent % │ read % sign (percent) percent
-
- When there are too many other users, there are too many other users, too many other users.
The preceding steps are very simple. % P and % N are rarely used. skip this step. To enter %, you must add another % in front of it. Let's focus on % s and % []. % S is to read an array. The difference between % s and gets is that % s will end with any null character, while gets will end with a carriage return. You can add a number before % s to indicate the number of read-only instances. The ansi c standard adds a new feature to scanf () called scan set ). The scan set defines a character set combination that can be read by scanf () and assigned to the corresponding character array. A scan set is defined by a string of characters in square brackets. It must be prefixed with a percent sign before the left square brackets. For example, the following scan set causes scanf () to read characters A, B, and C:
- % [ABC]
When using the scanning set, scanf () continuously eats the characters in the set and puts them in the corresponding character array until it finds that the characters are not in the Set (that is, the scanning set only reads matching characters ). When returned, the array contains a string ending with null and composed of read characters. For many implementations, a hyphen can be used to specify a range. For example, the following scan set allows scanf () to accept letters A to Z:
- % [A-Z]
It is important to note that the scan set is case sensitive. Therefore, if you want to scan uppercase and lowercase characters, you must specify uppercase and lowercase letters respectively. For % [], you can also use ^ + any character (including EOF) to end the string input. For example, % [^ EOF] is not aborted until there is an EOF input. But remember thatC LanguageIt is a buffer input. Even if you % [^ A], it is impossible to end the number of A inputs before you press Enter. % S will skip the white space, but % C will not. This is
- Scanf ("% D", & H );
- Scanf ("% C", & C );
If this is written, the variable C must be the carriage return. If you want to implement this input, you can add a getchar () between the two statements. He can eat the carriage return and scanf ("% d % C", & H, & C). Enter a number and add a space. But do not use scanf ("% d \ n", & H); K & R to make it clear that any non-formatted characters must be exactly matched. This means that it is valid to add \ n after the input number. There is also * before any item, indicating that the item does not match the value.
Http://www.zxbc.cn/html/20081126/68331.html