language: C (gcc-4.7.2)
date: 599 days 6 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
 
size_t st;
 
void PrintStackTop(const char *type)
{
    int stack_top;
    if(st == 0) st = (size_t) &stack_top;
    printf("In %s call version, the stack top is: %lu\n", type, (st - (size_t) &stack_top));
}
 
int TailCallFactorial(int n, int a)
{
    PrintStackTop("tail");
    if(n < 2)
        return a;
    return TailCallFactorial(n - 1, n * a);
}
 
int NormalCallFactorial(int n)
{
    PrintStackTop("normal");
    if(n < 2)
        return 1;
    return NormalCallFactorial(n - 1) * n;
}
 
 
int main(int argc, char *argv[])
{
    st = 0;
    printf("%d\n", TailCallFactorial(5, 1));
    st = 0;
    printf("%d\n", NormalCallFactorial(5));
    return 0;
}
 
prog.c: In function ‘PrintStackTop’:
prog.c:9: warning: format ‘%lu’ expects type ‘long unsigned int’, but argument 3 has type ‘size_t’