Is it possible to make C tail-recursive?
Maybe. Let's try.
In-Function Scope calling self
Ok, this is a limited case, but we can do it pretty easily.
int tail_recurse(int a, char *b, float c) {
restart_tail_recurse:
/*
* Your logic here.
*/
// you want to write this:
// tail_recurse(100, "wut", 10.3);
// but that uses the stack.
// to get proper tail recursive behavior,
// you can do this:
{
a=100, b="wut", c=10.3;
goto restart_tail_recurse;
// why is this in braces?
// so you can use some extra stack here that
// gets released on the goto
}
}
If you actually call this code as is, it creates an infinite loop . . . but hey, at least it doesn't blow up the stack :) It will probably blow up the CPU though. (Congratulations, you've substituted infinite time for infinite space!)
Cross Function Scope
lemme think about this.
Whole Program Scope
Hmmmmmmm..........