Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving "error: conflicting types for 'function'"

I am creating a program to convert from binary, decimal, hex, and oct to any one of those options. For hex, I need a way to format values greater than 9 into one of A, B, C, D, E, F. Since this will be repeated in several functions, I decided to make the following function:

char hexRepresentation(double n){
    if(n > 9){
        if(n==10) return 'A';
        if(n==11) return 'B';
        if(n==12) return 'C';
        if(n==13) return 'D';
        if(n==14) return 'E';
        if(n==15) return 'F';
    }

    return (char)n;
}

However, when I try to compile, I receive the error

conflicting types for 'hexRepresentation'

I'm utterly new at C, coming from Java, and am banging my head against a wall over what should be the simplest things to implement. Any help would be greatly appreciated!

like image 894
Thomas Preston Avatar asked Dec 26 '22 15:12

Thomas Preston


1 Answers

You don't get a declaration kind-of error because in C, when you do not forward declare a function most compilers assume an extern function that returns an int type. Actually the compiler should warn you about this (most do). Then later on when the compiler actually reaches the function implementation it finds a different return type, in this case a char, and then throws the "conflicting type" error. Just forward declare all the functions to avoid this kind of errors.

About what would be the best way somthing like following code woudl yield a similar result:

if (n > 9)
{
   return('A' + (n - 10));
}
like image 104
Julio Moreno Avatar answered Jan 10 '23 20:01

Julio Moreno