vNdong
vNdong

Reputation: 9

implicit declaration of function that should not be?

I am working with structs and typedef with an external file that works locally but on automatic checker I get implicit declaration of function ‘gcd’

this code consists of a couple functions that do basic stuff like return sums etc. A function gcd which was handed to us and counts the greatest common divisor and both of these codes/snippets are #included in a different main.c: #include "fraction.h" #include "fraction.c"

/* Reduce fraction */
void reduceFraction(Fraction *val) {
    unsigned int fr = gcd(val->numerator, val->denominator);
    val->numerator = val->numerator / fr;
    val->denominator = val->denominator / fr;
}

And this is the gcd which works

unsigned int gcd(unsigned int u, unsigned int v)
{
    // simple cases (termination)
    if (u == v)
        return u;

    if (u == 0)
        return v;

    if (v == 0)
        return u;

    // look for factors of 2
    if (~u & 1) // u is even
    {
        if (v & 1) // v is odd
            return gcd(u >> 1, v);
        else // both u and v are even
            return gcd(u >> 1, v >> 1) << 1;
    }

    if (~v & 1) // u is odd, v is even
        return gcd(u, v >> 1);

    // reduce larger argument
    if (u > v)
        return gcd((u - v) >> 1, v);

    return gcd((v - u) >> 1, u);
}

the error that appears is: implicit declaration of function ‘gcd’ [-Wimplicit-function-declaration]

Upvotes: 0

Views: 688

Answers (1)

IronMan
IronMan

Reputation: 1960

The warning is telling you that the function was not declared before being used. The function will be assumed to return an int with no restrictions on the type or number of parameters. You can eliminate this warning and enforce better type checking of this function by declaring the function in an included header file or earlier in the module:

unsigned int gcd(unsigned int u, unsigned int v);

Upvotes: 4

Related Questions