How can we calculate the stack and heap size requried by a C program.
Is there any specific formula used?
There is no general solution, since a C program can written which
uses a random amount of any resource.
Once you accept that sad fact, it's often possible to estimate
the usage by running the program multiple times, feeding it
different kinds and amounts of input, and observing its behavior
using operating-system specific tools.
Another method is to examine the program and add up the various
sizes of local variables and heap allocations. Some programs
are simple enough that you can simply see that they will use
a certain amount of a certain resource, constant or varying by
the amount or type of input. If there are is no recursion,
VLA's or alloca() calls, there should be a calculable upper
limit on stack space. If there are no malloc/calloc/realloc
calls, there should be a calculable upper limit on heap space.