The experiment program is compiled with vc6. Note that the file extension is C, not CPP. Download the previous test programs (linked list, table, atomic download link) and directly overwrite the following source program. C content!
Once again, we strongly recommend that you set a breakpoint on these functions and press F11 to repeat the source code to achieve the real learning effect!
The source program is as follows:
# Include <stdio. h> # include <string. h> # include "include/FMT. H "# include" include/Str. H "# pragma comment (Lib," libcii. lib ") typedef int (* fN) (INT, void *); void main () {// note: C language must put these variable declarations in the function header char * S1 = NULL; // The following is the [format] function // fmt_fmt (FN) fputc, stdout, "Test 1: fmt_fmt: % d, % s, % x, % F, % E, % G, single character: % C, hexadecimal: 0x % x \ n ", 9988," OK ", 9988, 90.89, 90.89, 90.89, 101,101);/* the following functions will eventually be called: when fmt_vfmt encounters [% d], it calls cvt_d. This static function has been installed during initialization, see the CVT [] initial assignment 'D' = 100 (ASCII value) in the Chinese version of p164. Corresponding to: cvt_d function 'c' = 100, corresponding to: cvt_c function 'E' = 101, 'F' = 102, 'G' = 103, all three are floating point conversion, and the subscript corresponds to the cvt_f function. For details, see the Chinese version p170 description, internally, the sprintf library function is called to format 'X' = 101, corresponding to: cvt_x function. Pay attention to the source code "* -- p =" 0123456789 abcdef "[M & 0xf]; "Section ;............. * /// fmt_print ("Test 2: fmt_print: % d, % s, % x \ n", 9988, "OK", 9988 ); // fmt_fprint (stdout, "Test 3: fmt_fprintf: % d, % s, % x \ n", 9988, "OK", 9988 ); // S1 = fmt_string ("Test 4: fmt_string: % d, % s, % x \ n", 9988, "OK", 9988); printf ("% s ", s1); // The following is the [conversion] function // fmt_register ('@', str_fmt);/* '@' = 64, associated with str_fmt, str_fmt and cvt_d, cvt_s, cvt_o, cvt_f, and so on are defined in the same way. Observe the internal implementation of str_fmt and accept the start offset and display length parameters, therefore, it is only applicable to the following fmt_print usage to compare the implementation of str_fmt and cvt_s functions (attached) */fmt_print ("Test 5: fmt_string: % @ \ n", "chinaokyes ", 0, 4 );}
Output
Test 1: fmt_fmt: 9988, OK, 2704, 90.890000, 9.089000e + 001,090.89, single character: E, hexadecimal: 0x65 Test 2: fmt_print: 9988, OK, 2704 Test 3: fmt_fprintf: 9988, OK, 2704 Test 4: fmt_string: 9988, OK, 2704 Test 5: fmt_string: naokyespress any key to continue
Compare the cvt_s (src/FMT. c) and str_fmt (src/Str. c) functions:
static void cvt_s(int code, va_list *app,int put(int c, void *cl), void *cl,unsigned char flags[], int width, int precision) {char *str = va_arg(*app, char *);assert(str);Fmt_puts(str, strlen(str), put, cl, flags,width, precision);}void Str_fmt(int code, va_list *app,int put(int c, void *cl), void *cl,unsigned char flags[], int width, int precision) {char *s;int i, j;assert(app && flags);s = va_arg(*app, char *);i = va_arg(*app, int);j = va_arg(*app, int);convert(s, i, j);Fmt_puts(s + i, j - i, put, cl, flags,width, precision);}
Appendix: the ASCII table is very useful and can be easily viewed:
The following table lists 0-127.
Code |
Char |
Code |
Char |
Code |
Char |
Code |
Char |
0 |
|
32 |
[Space] |
64 |
@ |
96 |
` |
1 |
|
33 |
! |
65 |
A |
97 |
A |
2 |
|
34 |
" |
66 |
B |
98 |
B |
3 |
|
35 |
# |
67 |
C |
99 |
C |
4 |
|
36 |
$ |
68 |
D |
100 |
D |
5 |
|
37 |
% |
69 |
E |
101 |
E |
6 |
|
38 |
& |
70 |
F |
102 |
F |
7 |
|
39 |
' |
71 |
G |
103 |
G |
8 |
** |
40 |
( |
72 |
H |
104 |
H |
9 |
** |
41 |
) |
73 |
I |
105 |
I |
10 |
** |
42 |
* |
74 |
J |
106 |
J |
11 |
|
43 |
+ |
75 |
K |
107 |
K |
12 |
|
44 |
, |
76 |
L |
108 |
L |
13 |
** |
45 |
- |
77 |
M |
109 |
M |
14 |
|
46 |
. |
78 |
N |
110 |
N |
15 |
|
47 |
/ |
79 |
O |
111 |
O |
16 |
|
48 |
0 |
80 |
P |
112 |
P |
17 |
|
49 |
1 |
81 |
Q |
113 |
Q |
18 |
|
50 |
2 |
82 |
R |
114 |
R |
19 |
|
51 |
3 |
83 |
S |
115 |
S |
20 |
|
52 |
4 |
84 |
T |
116 |
T |
21 |
|
53 |
5 |
85 |
U |
117 |
U |
22 |
|
54 |
6 |
86 |
V |
118 |
V |
23 |
|
55 |
7 |
87 |
W |
119 |
W |
24 |
|
56 |
8 |
88 |
X |
120 |
X |
25 |
|
57 |
9 |
89 |
Y |
121 |
Y |
26 |
|
58 |
: |
90 |
Z |
122 |
Z |
27 |
|
59 |
; |
91 |
[ |
123 |
{ |
28 |
|
60 |
< |
92 |
\ |
124 |
| |
29 |
|
61 |
= |
93 |
] |
125 |
} |
30 |
- |
62 |
> |
94 |
^ |
126 |
~ |
31 |
|
63 |
? |
95 |
_ |
127 |
|