for
您是否检查过循环执行了多少次?在你的for
循环中,number
以指数比例增加。我写了一个小程序来展示这种情况。
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4
5 #define MAXLOOP 100
6
7 int
8 main(void)
9 {
10 uint64_t number = 1U;
11 int i;
12
13 for(i = 0; i < MAXLOOP; ++i){
14 number *= 10;
15 printf("%d\t%lu\n", i, number);
16 }
17
18 return 0;
19 }
20
输出是:
0 10
1 100
2 1000
3 10000
4 100000
5 1000000
6 10000000
7 100000000
8 1000000000
9 10000000000
10 100000000000
11 1000000000000
12 10000000000000
13 100000000000000
14 1000000000000000
15 10000000000000000
16 100000000000000000
17 1000000000000000000
18 10000000000000000000
19 7766279631452241920 <--- overflow, uint64_t can store 18,446,744,073,709,551,616 as its max value
20 3875820019684212736
21 1864712049423024128
22 200376420520689664
23 2003764205206896640
24 1590897978359414784
25 15908979783594147840
26 11515845246265065472
27 4477988020393345024
28 7886392056514347008
29 5076944270305263616
30 13875954555633532928
31 9632337040368467968
32 4089650035136921600
33 4003012203950112768
34 3136633892082024448
35 12919594847110692864
36 68739955140067328
37 687399551400673280
38 6873995514006732800
39 13399722918938673152
40 4870020673419870208
41 11806718586779598848
42 7386721425538678784
43 80237960548581376
44 802379605485813760
45 8023796054858137600
46 6450984253743169536
47 9169610316303040512
48 17909126868192198656
49 13070572018536022016
50 1578511669393358848
51 15785116693933588480
52 10277214349659471872
53 10538423128046960640
54 13150510911921848320
55 2377900603251621888
56 5332261958806667264
57 16429131440647569408
58 16717361816799281152
59 1152921504606846976
60 11529215046068469760
61 4611686018427387904
62 9223372036854775808
63 0
64 0
65 0
66 0
67 0
68 0
69 0
70 0
71 0
72 0
73 0
74 0
75 0
76 0
77 0
78 0
79 0
80 0
81 0
82 0
83 0
84 0
85 0
86 0
87 0
88 0
89 0
90 0
91 0
92 0
93 0
94 0
95 0
96 0
97 0
98 0
99 0
我想你很容易理解。:->