Besides the bugs of return
and exit
you have a bug also in the way you use ints and characters. isdigit
is a function that is only applied to characters, giving true if a character is between '0'
and '9'
, but one should know that the character notation in C is only a fancy way of writing a codepoint (ASCII most of the time). So when you write '1'
in a C program, the compiler will see 49
, if you write 'a'
the compiler sees in reality 97
. This means that isdigit
return true
for the values 48
to 57
, probably not what you intended. In the line where you compare divider
with '1'
, in reality you're comparing it with 49
(except on IBM mainframe, where '1'
is 241
)
Your loop is infinite, it depends on the value of x, but x isn't changed in the loop, so the condition in the while can never change.
EDIT: Here the corrected code
int is_prime(uint_t x)
{
uint_t divider;
if(x <= 3)
return 1;
for(divider = x-1; ; divider--) {
if(x % divider == 0)
return 0; //not prime
if(divider == 1)
return 1; //if divider reaches 1 then the number is prime
}
}