Here is the sample code which is giving segmentation fault error
#include<stdio.h>
void fun ();
int main() {
fun ();
return 0;
}
void fun () {
int i;
for (i=0;i <4;i++) {
fun ();
printf ("%d",i);
}
}
Here is the sample code which is giving segmentation fault error
#include<stdio.h>
void fun ();
int main() {
fun ();
return 0;
}
void fun () {
int i;
for (i=0;i <4;i++) {
fun ();
printf ("%d",i);
}
}
In the following code
void fun () {
int i;
for (i=0;i<4;i++) {
fun ();
printf ("%d",i);
}
}
You are doing an infinite recursion. The function fun()
calls itself and there is no exit condition for the recursion. So eventually the function stack will get exhausted and you will get segmentation fault.
This is because you are getting in an infinite recursion which leads to stack overflow. You have to add a base case for your recursive function to exit.