Search code examples
cfunctionconflictpow

Error: Conflicting types for 'pow' in C


The program is supposed to get letters both uppercase and lowercase from user and each letter is assigned to a value.

Example. S is 0, K is 1 and E is 2.
The totalvalue formula is ((A*10)+(B))*10^C

A - first input B - 2nd input C is the 3rd input.
The function int lettervalue(char letter) is supposed to return the value of the letter. I used switch construct for assigning the letter value.

Everytime i compile I get error: conflicting types for 'pow'. Does the pow function accept variables, and is there something I should change in function int lettervalue(char letter)?

    #include <stdio.h>
    #include <math.h>

int pow(int x, int y);

int lettervalue(char letter)
{
    int val;
    switch (letter){
        case 'S':
        case 's':
            val = 0;
            break;
        case 'K':
        case 'k':
            val = 1;
            break;
        case 'E':
        case 'e':
            val =2;
            break;
    }
    return val;
}

int main(void)
{   int valueA, valueB, valueC, totalvalue;
    char A, B, C;
    printf("Please input the letter: ");
    scanf("%c%c%c", &A, &B, &C);                
    valueA = lettervalue(A)*10;
    valueB = lettervalue(B);
    valueC = lettervalue(C);
    totalvalue = (valueA+valueB)*pow(10, valueC);
    printf("The total value is %d", totalvalue );

    return 0;
}

Solution

  • Code unnecessarily provides a pow() prototype, which is inconsistent with the one in math.h.

    #include <math.h>
    // math.h provides: double pow(double x, double y);
    

    Eliminate int pow(int x, int y);.

    Change code

    // totalvalue = (valueA+valueB)*pow(10, valueC);
    totalvalue = (valueA+valueB)* ((int)pow(10, valueC));
    

    Or

    double total;
    total = (valueA+valueB)*pow(10, valueC);
    printf("The total value is %.0lf", total );
    

    Be sure to link in the math library to avoid 'undefined reference to pow'.

    "undefined reference to `pow'" even with math.h and the library link -lm