Implicit declaration of function x
Asked Answered
F

4

5

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;
}
Fluvial answered 24/7, 2013 at 16:45 Comment(1)
A comment would be nice in regards to the downvote.Fluvial
S
12

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).

Semilunar answered 24/7, 2013 at 16:48 Comment(8)
Thanks for the suggestion. I've been refactoring this code for the last few hours to solve this project euler problem; it's probably not my best code.Fluvial
@Fluvial No problem BlueCat! I just wanted to share what I encountered hence added. You Welcome!Semilunar
Yes, I'm aware of the literals in C. Thank you, though. I guess it's useful anyway.Fluvial
The L suffix on the constant is unnecessary. The constant will be an integer type sufficient to represent the value, and it will converted to long for passing to the function. A bigger concern is that the format specifier is %d, which is for int, but the value passed for it has type long. Additionally i % 1 == 0 and i % i == 0 are each always true (except the latter is undefined if i is zero).Podagra
@EricPostpischil get it! Understood first message as well as second. Eric You should be a teacher! :) Thanks .. your comments are very helpful (not only for answering, but also for me) I'm learning from you new things.Semilunar
@EricPostpischil Nice! good pics can I add in my answer these pointsSemilunar
As I said it's extremely rushed code. I had a batter version that did not work, so I didn't post it.Fluvial
@Fluvial no problem man! I am happy that I got a nice badge for what I am looking for. Thanks mat!Semilunar
F
2

Typo. The declaration says largeTSprime. Change it to the correct largestprime and it will work.

ProTip #1: use camelCapsOnWordBoundaries or under_scores for readability.

ProTip #2: it's almost never a compiler bug.

Fantoccini answered 24/7, 2013 at 16:49 Comment(4)
well, 99.99% of the time.Fluvial
@Fluvial Noticed 'almost'?Fantoccini
I wanted to quantify it more; gcc is pretty stable, especially on stable distros.Fluvial
@Fluvial Ah, OK; misinterpreter that. Sorry.Fantoccini
C
1

You have a typo in the prototype. It should be largestprime instead of largetsprime.

Cyanocobalamin answered 24/7, 2013 at 16:48 Comment(0)
F
1

You have a typo in the prototype:

largetsprime != largestprime
Flank answered 24/7, 2013 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.