F
Francine.Neary
./foo
Segmentation fault (core dumped)
./foo -h
1
Well, of course, you could bloat the code with error messages, but
there's the old saying, Garbage In Garbage Out (GIGO).
./foo 0 /* it actually gets this right, which was a mild surprise */
1
Every non-negative integer argument up to and including 12 works fine,
but observe what happens when we exceed 12:
./foo 13
1932053504
(the correct value is of course 6227020800).
Well, you can modify the struct to maintain the factorial as a long
long if you want...
So you have a seriously convoluted program which only works for a
vanishingly small percentage (less than 0.2%) of possible valid inputs.
My own factorial program is, alas, twice as long as yours (58 lines),
not including library code of course - but it continues to get the
answers right long past 13, despite not taking advantage of ISO's
licence to write into argv. For example, compare this:
./foo 52
0
with this:
~/path/to/rjhfactorial 52
80658175170943878571660636856403766975289505440883277824000000000000
....or even to use GMP or some other library, and it will then work for
100% of valid inputs. This was just a proof of concept - like anything
it can be extended and generalized out of sight.