Showing posts with label segmentation fault (core dumped). Show all posts
Showing posts with label segmentation fault (core dumped). Show all posts

Saturday, 15 December 2012

SEGMENTATION FAULT (CORE DUMPED) IN C

Common reason why you would get such an error message despite of no syntax error being spotted after compilation are:

1)you are not passing the memory address of the variable to a scanf statement. for example:

int n;
scanf("%d",n); // wrong statement
scanf("%d",&n);//correct statement
In case of a string ,it is not necessary to place '&' :
char ans[10];
scanf("%s",ans);//correct statement

2)your user defined function is falling into an infinite loop;

void recur(int x)
{
recur(x);

3)if you are playing with strings in your code the it is preferable that you use fflush(stdin) after every input statement;

scanf("%s",ans);
fflush(stdin);