Day#49 'C' Coding Challenge

Day#49 'C' Coding Challenge

main() function cannot be called recursively? Do you agree with this statement?

A recursive function is something which call itself. Example as below:

int sum(int x)
{
    x = x+1;
    if (x > 100)
        break; 
    sum(x);
}

Here sum() is called from within the same sum() function. Even the below declaration is valid where main() calls itself but calling main continuously leads to stack overflow.

void main()
{
    int x;
    x = x+1;
    main();
}

While there is a restriction of the implementation of the above, it is just written to show that main() function can be called recursively.

While the recursive functions can be used without any issues, there is a problem with stack overflow that might occur. When the function is called continuously, the return address and the associated arguments with that recursive call are stored in Stack. Excessive recursive calls or infinite recursive calls fill up the finite stack space and causes a condition called stack overflow. 

For beginners, stack is nothing but a space in RAM whose allocation is managed by operating system.

Post a Comment

0 Comments