Hello, I'm working on an exercise in my book. It tells you to write a program that will print out the first n prime numbers, where n is inputed by the user.
My output is should like this (which it does):
Primes
--------
2
3
5
7
...
The program should have 3 files, a prime.h which has all the preprocessing directives, a is_prime.c file which has the function to determine if a number is prime and prime.c which contains the main function.
Here's what all three looks like:
prime.h
Code:
#include <stdio.h>
#include <stdlib.h>
int is_prime( int n );
is_prime.c
Code:
#include "prime.h"
int is_prime( int n ){
int k, limit;
if( n == 2) return 1;
if( n % 2 == 0) return 0;
limit = n / 2;
for( k = 3 ; k <= limit ; k += 2 )
if( n % k == 0 ) return 0;
return 1;
}
and finally prime.c (the main program)
Code:
#include "prime.h"
int main(void){
int i=2, n, count=0;
printf("Howw many prime number do you want to see? ");
scanf("%d", &n );
printf("Primes\n------\n");
while(1){
if( is_prime( i ) ){
count++;
printf("%3d: %d \n", count, i);
}
i++;
if( count == n ) break;
}
return 0;
}
My problem is, when I compile with these 3 files I get an error:
Unresolved external '_is_prime' referenced from prime.obj
but if I put all the code in one file it works fine, what am I doing wrong?