0

I'm working with code (that I am not the primary author of) and trying to get it to run.

It tests to see if a number is prime or not.

It gives me the error error: "control may reach end of non-void function" and specifies line 34, the final line of the code.

How do I fix this? Thank you!

#include <stdio.h>
#include <math.h>

int check_prime(int);

int main()
{
int n, result;

  printf("Enter an integer to check whether it is prime or not.\n");
  scanf("%d",&n);

   result = check_prime(n);

  if ( result == 1 )
      printf("%d is prime.\n", n);
  else
       printf("%d is not prime.\n", n);

  return 0;
  }

  int check_prime(int a)
  {
  int c;

  for ( c = 2 ; c <= a - 1 ; c++ )
  { 
  if ( a%c == 0 )
  return 0;
  }
  if ( c == a )
  return 1;
  }
1

Simply put, your check_prime() function has all of the return statements inside if statements. The compiler believes it is theoretically possible (even if not logically possible - remember, compilers are stupid) that the function could execute without ever hitting a return statement. It treats this as a warning, and the make command says to treat all warnings as errors.

There should be at least one return statement that will execute without having to satisfy a condition, as a catchall return. Generally, if the function is looking to validate that something is true, then the catchall should return false. If returning other types, you'll need to consider what is appropriate.

For more info, How to solve "control may reach end of non void function" error

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged .