I get segmentation fault core dumped. My function is supposed to return the prime number following the one put in parameters, or to return the number if it is a prime.
When I compile, I have no errors, I also compiled with -Wall, it worked. But when I run the program I get segmentation fault, core dumped. I can't see the issue.
#include <stdio.h>
int ft_next_prime(int nb)
{
int i = 2;
int notPrime = 0;
while (i++ < 9 || notPrime != 1) {
if (nb == i)
i++;
else if (nb % i == 0)
notPrime = 1;
}
return ((notPrime) ? ft_next_prime(nb++) : nb);
}
int main()
{
printf("%d", ft_next_prime(15));
}
why?
Also, can I write this?
while (i++ < 9 || !notPrime)
if (nb % (i != nb) == 0)
notPrime = 1;