zsh: segmentation fault in C after compile - c

I have an program in C for showing appropiate number prime (max or min). After input a value, i have error "zsh:segmentation fault". Why?
Thanks.
#include <stdio.h>
#include <stdlib.h>
int max(int n){
for (int i=2;i<=n;i++)
if(n%i == 0)
return max(n+1);
return n;
}
int min(int n){
for(int i=2;i<=n;i++)
if(n%i==0)
return min(n-1);
return n;
}
int main(){
int n;
printf("Numarul =");scanf("%d",&n);
printf("Numarul anterior prim numarului dat este %d",min(n));
printf("Numarul urmator prim numarului dat este %d",max(n));
}

Related

Segmentation Fault while printing

#include <stdlib.h>
#include <stdio.h>
#include "string.h"
//This function tries to calculate result of the floor function for floats <= 9999.
int main(int argc, char* argv[]) {
int i, j;
float k;
int x[10000];
for(i = 0; i < 10000; ++i){
x[i] = i;
}
printf("Enter a float in 0..9999: ");
scanf("%f", &k);
tester(x, k);
}
int tester(int* c, int k) {
printf("x[%d] = %d\n", k, c[k]);
}
When I run the program it gives me segmentation fault in here:
printf("x[%d] = %d\n", k, c[k]);
Can anyone see the what problem really is?
You can see the screenshots:
segmentation fault in printf
There are two major problems in your code.
You get input from the user (scanf) as float, but actually use it as int to pass it into the function and as index for the array x.
You should ALWAYS check user input from the terminal (or wherever). In this case the check should be at least, if the actual input is between 0 and 9999.
Improved version:
#include <stdlib.h>
#include <stdio.h>
#include "string.h"
void tester(int* c, int k) {
printf("x[%d] = %d\n", k, c[k]);
}
//This function tries to calculate result of the floor function for floats <= 9999.
int main(int argc, char* argv[]) {
int i;
int k;
int x[10000];
for(i = 0; i < 10000; ++i){
x[i] = i;
}
printf("Enter a float in 0..9999: ");
scanf("%d", &k);
if (k >= 0 && k < 10000) {
tester(x, k);
} else
{
printf("Sorry, your input %d was invalid.", k);
}
}
Probably that will fix your Segmentation Fault problem.

can not run the simple function program

#include <stdio.h>
#include <conio.h>
int sum();
//find the sum of two numbers entered by the user.
int main(){
int n, m;
printf("Enter two numbers: \n");
scanf("%d", &m);
scanf("%d", &n);
int result = sum(m, n);
printf(result);
getch();
return 0;
}
int sum(m, n){
int c;
c = m+n;
return c;
}
i was just writing a simple program with function but i don't know why it is not running it tells me to debug can someone tell me what is the problem with it
Change int sum() ; to int sum(int, int) ;
Change printf(result) to printf("%d", result) ;
Change int sum(m, n) to int sum(int m, int n) ;(https://i.stack.imgur.com/Z07cx.jpg)
Another way of writing above program is
(https://i.stack.imgur.com/VOXRY.jpg)
#include <stdio.h>
#include <conio.h>
int sum(int n, int m);
//find the sum of two numbers entered by the user.
int main(){
int n, m;
printf("Enter two numbers: \n");
scanf("%d", &m);
scanf("%d", &n);
int result = sum(m, n);
printf("%d",result);
getch();
return 0;
}
int sum(int n, int m){
int c;
c = m+n;
return c;
}

Why only codeblocks give me return error(0xc00000FD)?

When I compile the program with Codeblocks (GNU gcc compiler) it gives me this error: Process returned -1073741571 (0xC00000FD)
I tried it without the binary search and it works, but I can't understand why when I insert the binary search it gives me this error.
But if I compile it with another compiler (I tried on mine university online compiler) it works very well.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
int v[n];
int i;
int aux;
int j;
printf("Quanti numeri vuoi inserire? \n");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("%d: ",i);
scanf("%d",&v[i]);
}
for(i=0;i<n;i++) {
printf("%d,",v[i]);
}
printf("\n");
for(j=0;j<n-1;j++) {
for(i=0;i<n-1;i++) {
if(v[i]>v[i+1]) {
aux = v[i];
v[i] = v[i+1];
v[i+1] = aux;
}
}
}
for(i=0;i<n;i++)
printf("%d ", v[i]);
//Binary search
int alto, basso, pos, ele, k;
alto = 0;
basso = n-1;
pos = -1;
printf("Mi dia un elemento da cercare:\n");
scanf("%d",&ele);
while(alto<=basso && pos==-1){
k=(alto+basso)/2;
if(v[k]==ele)
pos=k;
else
if(v[k]<ele)
alto=k+1;
else
basso=k-1;
}
if(pos==-1)
printf("Il numero inserito non e' presente");
else
printf("L'elemento e' presente in posizione %d del vettore", pos);
return 0;
}
int n;
int v[n];
you have undefined behaviour(UB) here.
you declare a VLA with the size set by not initialized variable n.
So answering the question
Why only codeblocks give me return error(0xc00000FD)
the answer is : because it does. It is not predictable.

reversing elements of an integer array

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int a[20],int n);
int main()
{
int a[20];
int n;
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the array elements\n");
for(int i=0;i<n;i++)
{
scanf("%d ",&a[i]);
}
reverse(a,n);
return 0;
}
int reverse(int a[20],int n)
{
for(int i=n-1;i>=0;i--)
{
printf("%d ",a[i]);
}
return 0;
}
here if I input n=4 then during runtime i have to take 5 elements and then it reverses.For eg if i take n=4 and then for no of elements i have to take 1,2,3,4,5 and then only output is coming as 4 3 2 1.Why? is my logic wrong? also in this code I am unable to take the number of elements of arrays in a straight line, like 1 2 3 4.When I am entering the number each number is entering in new line .I am a novice programmer in C and thus having these doubts.Please anyone explain...
The problem with your code is the extra space after %d in your scanf line where you accept array elements i.e.
for(int i=0;i<n;i++)
{
scanf("%d ",&a[i]); //should be scanf("%d",&a[i]);
}
Change that and you're good to go.
Here is your entire program refactored to work correctly:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int a[20], int n)
int main()
{
int a[20];
int n;
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the array elements\n");
for (int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
reverse(a,n);
return 0;
}
int reverse(int a[20], int n)
{
int mid = n/2;
for (int i=0; i < mid; ++i)
{
int temp = a[n-i-1];
a[n-i-1] = a[i];
a[i] = temp;
}
return 0;
}

c-Segmentation Fault:11

The code is to find all possible combination of numbers present in the array a that would add up to a given number n. I am getting segmentation fault in this piece of code.
The error is Segmentation Fault:11.
Please HELP.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void comb(long long int n,int a[],int k);
long int ct=0;
int main() {
long long int n;
scanf("%lld",&n);
int k;
scanf("%d",&k);
int a[k];
for(int i=0;i<k;i++)
scanf("%d",&a[i]);
comb(n,a,k);
ct=ct*2;
printf("%ld",ct);
return 0;
}
void comb(long long int n,int a[],int k)
{
if(n==0)
{
ct++;
return;
}
else
if(n<0)
return;
else
{
for(int j=0;j<k;j++)
{
comb(n-a[j],a,k);
}
}
}
The problem is that scanf is not safe. Use fgets and add some error checking.
For example if user types a letter on scanf("%lld", &n); your program crashes with seg fault.

Resources