// test_factorial_long.c the simplest example of a recursive function // a recursive function is a function that calls itself // external // long int factorial_long(long int n) // n! is n factorial = 1*2*...*(n-1)*n // { // if( n <= 1 ) return 1; // must have a way to stop recursion // return n * factorial_long(n-1); // factorial calls factorial with n-1 // } // n * (n-1) * (n-2) * ... * (1) #include "factorial_long.h" #include int main() { printf("test_factorial_long.c using long int, note overflow\n"); printf(" 0!=%ld \n", factorial_long(0)); // Yes, 0! is one printf(" 1!=%ld \n", factorial_long(1l)); // Yes, "C" would convert 1 to 1l printf(" 2!=%ld \n", factorial_long(2l)); // because of function prototype printf(" 3!=%ld \n", factorial_long(3l)); // coming from factorial_long.h printf(" 4!=%ld \n", factorial_long(4l)); printf(" 5!=%ld \n", factorial_long(5l)); printf(" 6!=%ld \n", factorial_long(6l)); printf(" 7!=%ld \n", factorial_long(7l)); printf(" 8!=%ld \n", factorial_long(8l)); printf(" 9!=%ld \n", factorial_long(9l)); printf("10!=%ld \n", factorial_long(10l)); printf("11!=%ld \n", factorial_long(11l)); printf("12!=%ld \n", factorial_long(12l)); printf("13!=%ld \n", factorial_long(13l)); printf("14!=%ld \n", factorial_long(14l)); printf("15!=%ld \n", factorial_long(15l)); printf("16!=%ld \n", factorial_long(16l)); printf("17!=%ld \n", factorial_long(17l)); printf("18!=%ld \n", factorial_long(18l)); printf("19!=%ld \n", factorial_long(19l)); printf("20!=%ld \n", factorial_long(20l)); printf("21!=%ld \n", factorial_long(21l)); /* expect a problem with */ printf("22!=%ld \n", factorial_long(22l)); /* integer overflow */ return 0; } /* output of execution, with comments, is: test_factorial_long.c using long int, note overflow 0!=1 1!=1 2!=2 3!=6 4!=24 5!=120 6!=720 7!=5040 8!=40320 9!=362880 10!=3628800 11!=39916800 12!=479001600 13!=6227020800 14!=87178291200 15!=1307674368000 16!=20922789888000 17!=355687428096000 18!=6402373705728000 19!=121645100408832000 20!=2432902008176640000 21!=-4249290049419214848 wrong and obvious if you check your results 22!=-1250660718674968576 */