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;
}
TheBlueCat
  • 1,147
  • 5
  • 19
  • 38

4 Answers4

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

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • 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. – TheBlueCat Jul 24 '13 at 16:59
  • 1
    @TheBlueCat No problem BlueCat! I just wanted to share what I encountered hence added. You Welcome! – Grijesh Chauhan Jul 24 '13 at 17:00
  • 1
    Yes, I'm aware of the literals in C. Thank you, though. I guess it's useful anyway. – TheBlueCat Jul 24 '13 at 17:02
  • 4
    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). – Eric Postpischil Jul 24 '13 at 18:31
  • @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. – Grijesh Chauhan Jul 24 '13 at 18:39
  • @EricPostpischil Nice! good pics can I add in my answer these points – Grijesh Chauhan Jul 24 '13 at 18:41
  • As I said it's extremely rushed code. I had a batter version that did not work, so I didn't post it. – TheBlueCat Jul 25 '13 at 10:15
  • @TheBlueCat no problem man! I am happy that I got a nice badge for what I am looking for. Thanks mat! – Grijesh Chauhan Jul 25 '13 at 14:09
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.

1

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

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

You have a typo in the prototype:

largetsprime != largestprime
Geoffrey
  • 10,843
  • 3
  • 33
  • 46