Implicit declaration of function x
algorithm, c, prime-factoring
Solution
Point-1 You have misspelled `largest` in function name
long largetsprime(long)
^
s is wrong here
In declaration It should be
long largestprime(long)
^ before t
Point-2 You are using `sqrt()` library function from `math.h`, you should compile your program with `-lm` as:
gcc -Wall -std=c99 -pedantic primefactors.c -lm
Point-3 You are returning `int` whereas return type of your function is `long`.
Point-4 One more mistake suggestion in call of `printf()` you forgot adding suffix for `long int`.
largestprime(600851475143)
should be:
largestprime(600851475143L)
// ^ added suffix L for long
If you are not aware of suffix `L` then read: What does the “L” mean at the end of an integer literal?
Thanks to @Eric Postpischil:
point-5: `printf()` in `main()` function is printing `long` type integer whereas you have used `%d` format specifier to print it:
printf("%d\n", largestprime(600851475143));
^
|
returns long
use `%ld` instead.
point-6:
`if`-condition in largest prime function `i % 1 == 0 and i % i == 0` are each always true (except the latter is undefined if `i` is zero) because `i % 1` = `0` (every number is divisible by `1`).
Problem
I'm well aware of function prototypes, this error seems to be a function declaration error, which means I'm really bewildered as to why I'm see this warning and thus error. It's almost like gcc completely ignores my function prototype. Is this a compiler bug? In the interest of brevity, I did not declare this function in a separate header file, though it should make no difference. gcc output: ``` $ gcc -Wall -std=c99 -pedantic primefactors.c primefactors.c: In function ‘main’: primefactors.c:8:5: warning: implicit declaration of function ‘largestprime’ [-Wimplicit-function-declaration] primefactors.c: At top level: primefactors.c:12:6: error: conflicting types for ‘largestprime’ primefactors.c:8:20: note: previous implicit declaration of ‘largestprime’ was here ``` code: ``` #include <stdio.h> #include <math.h> long largetsprime(long); int main() { printf("%d\n", largestprime(600851475143)); return 0; } long largestprime(long num) { int highest; int mid = sqrt(num); for (int i = 2; i < mid; i++) { if (mid % i == 0) { if (i % 1 == 0 && i % i == 0) highest = i; } } return highest; } ```