i`m trying to code some recursive algorithm and run into a segmentation fault upon executing the program. since i`m quite new to programming, especially in c, i wrote the following piece of code just to find out how far i can go with, what looks to me, the simplest recursive function there is.
the thing is that on my machine i get to 523797 after which the segmentation fault appears and i would have thought that it should go much farther than that.Code:#include <stdio.h> void x(long a) // the recursive function which calls itself in order to increase a by one { if(a<1000000){ a=a+1; printf("%.5d\n", a); x(a); } else return; } int main() { long b=1; x(b); return 0; }
Could anyone explain to me why the segmentation fault appears (i guess its a memory thing) cos i don`t really see why it occurs at such an early stage.
thx a million in advance.