tags:

views:

139

answers:

6

When I use a debugger I can tell the exit is not exiting the function. Am I using the exit function wrong? (i must be) how do I fix this?

int is_prime(int x,char array[]){
int divider = (x-1);   
float test;

  while(x>-1){
  test = isdigit((x % divider));  //isdigit returns !0 if digit
    if(divider == '1'){
    return(1);  //if divider reaches 1 then the number is prime
    exit;
    } 
    if(test  == '0'){
    return (0);//not prime
    exit;
    }
  divider--;
  }

}

+1  A: 

Read exit(3)'s manual.

Bertrand Marron
+2  A: 

You must call it:

exit(0);

Also, if you put it after return, it will never be called, since return returns from the function.

EDIT: And, as others have said, exit exits the program, so you probably don't want to use exit here.

Thomas Padron-McCarthy
So if I put it before return, will return still work?
pisfire
pisfire: No, exit terminates the program, so the return after it will not happen.
Thomas Padron-McCarthy
@pisfire what do you think `exit` does?
Bertrand Marron
I thought it would exit the function.
pisfire
@pisfire: that's what `return` does.
jweyrich
A: 

exit exits the process, not the function. You want return.

Oli Charlesworth
+5  A: 

The name of a function by itself (with no parenthesis after it) just gives you the address of a function without calling it. In C the most basic statement is an expression which is evaluated for its side effects, with the resulting value of the expression ignored. So a statement like exit; or 3; which has no side effects is legal but doesn't actually do anything and might as well be deleted. Some compilers will give you warnings about such meaningless statements, though you may have to turn on extra diagnostic warnings to get them. Using such options (such as -Wall for gcc) is a very good idea and will help you avoid some pitfalls like this.

Chris Dodd
+1  A: 

The statement:

exit;

gives the following warning with GCC:

C:\temp\test.c:71: warning: statement with no effect

What happening is that you have an expression that evaluates to the address of the exit() function - but that expression doesn't actually do anything with that address. it's similar to if you had a statement like:

1 + 2;

It's valid C, but it has no effect on anything.

To call the function, as Thomas Padron-McCarth said, you have to have the argument list (even if they're empty for some functions):

exit(0);
Michael Burr
+1 for explaining why the `exit;` lines are not syntax errors. There is also the fact that the evaluations of the `exit()` function address are both immediately after a return statement, so the 'statement with no effect' is also unreachable code :)
Jonathan Leffler
@Jonathan: even though I was in a bit of a rush at the time, I can't believe I didn't notice that...
Michael Burr
+1  A: 

Besides the bugs of returnand 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

  }
}
tristopia
@tristopia Is there a function in c that does, what I thought isdigit did?
pisfire
no, you don't need a function. If you assign the result of your modulo operation to the variable `test` you will already have what you want. I ake an edit to my post with the right answer.
tristopia
I modified a bit the progra using the same algorithm. I made the variable unsigned so that we do not need to handle negative numbers. I used a for loop without end condition as all conditions are inside the loop. I also added a check for values below 3, which would not work properly otherwise.
tristopia