r/C_Programming • u/yug_jain29 • Jul 10 '26
Question can I skip recursive functions?
a code i can run with the logic of iterative, so why do I have to learn the new concept as complicated as recursive? (ik it's one of important questions in c)
if yes will u pls explain it with a very realistic and simple example
thanks a lot đ
0
Upvotes
View all comments
Show parent comment
2
u/WittyStick Jul 11 '26 edited Jul 11 '26
The C standard makes no such assumptions - it doesn't even assume a stack!
The stack is a de-facto standard implementation for automatic storage duration that virtually all compilers use, but is implementation-defined and there are differences in how compilers represent it. Some of those implementations can guarantee tail recursion won't blow up the stack (eg, recent versions of GCC/Clang via a
musttailattribute - though there are limitations - such as the caller and callee requiring the same signature).The example you've chosen for Haskell is probably not the best demonstration because it is not tail-recursive -
evaluatemust return before+or*are applied, so the implementation still needs storage space for intermediate results. Functional languages typically resolve this by making all calls into tail calls - by converting to continuation-passing-style. CPS is much more cumbersome in C due to lack of standard closures - we need to implement our own closures even if usingmusttailto implement an evaluator like the above without growing the stack. Withoutmusttail, we can still implement CPS using for example, a trampoline.