Error ": conflicting types for" function ""

I am creating a program to convert from binary, decimal, hexadecimal and octa to any of these parameters. 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 get an error

conflicting types for 'hexRepresentation'

I am brand new in C, coming with Java, and banging my head against a wall over what should be the easiest to implement. Any help would be greatly appreciated!

+4
source share
3 answers

You don’t get the type of error declaration because in C, when you do not forward the declare function, most compilers accept an extern function that returns an int type. In fact, the compiler should warn you about this (most often). Then, when the compiler really reaches the implementation of the function, it finds a different return type, in this case char, and then throws a "conflicting type" error. Just forward all functions to avoid such errors.

That would be better than the following woudl code would give a similar result:

 if (n > 9) { return('A' + (n - 10)); } 
+9
source

Whatever you do in the submitted code, you are not using the double data type at all, inside the function. And from the return type of the function, it looks like you will never see the return value> 127.

This code highlights some issues: (This is just illustrative)

 char hexRepresentation(double n){ if(n > 9){//comparing (double>int). Bad. if(n==10) return 'A'; if(n==11) return 'B'; if(n==12) return 'C';//You wrote if(double == int). Bad. if(n==13) return 'D'; if(n==14) return 'E'; if(n==15) return 'F'; } return (char)n; //again unsafe downgrade from 8 bytes double to 1 byte char. } 

Even if you fix a compiler error, you may not always get the desired results because of such a dangerous use of the data type in a function.

To find out why this is bad, look here:

http://www.cygnus-software.com/papers/comparingfloats/Comparing%20floating%20point%20numbers.htm

I would use fabs(n) instead of n everywhere in this function body.

And the error "conflicting types for" hexRepresentation "will be shown if there is a preliminary declaration or definition of the same named hexRepresentation function before this function hexRepresentation . In addition, if you do not declare the function and it appears only after the call, it is automatically considered an int compiler.

So, declare and define your function before main () or declare it before main () and define the function elsewhere in the file, but with the same function prototype.

Do:

  char hexRepresentation(double); //Declaration before main main() { ... } char hexRepresentation(double n){//Definition after main ... } 

or

  char hexRepresentation(double n){ //Declaration and definition before main ... } main() { ... } 
+1
source

Just create one stack (for char) that has push and pop functions. I am not returning a value by simply printing it there in the same function. (implemented only for integer values)

 #define max 100 char a[max]; void hex(int n) { while(n>0) { int rem = n%16; if (rem<10) push(rem+'0'); elsif(rem >=10 && rem <16) { switch(rem) { case 10: push('A');break; case 11: push('B');break; case 12: push('C');break; case 13: push('D');break; case 14: push('E');break; case 15: push('F');break; } else n=n/16; } } i=0; while(top>-1) a[i++]=pop(); i=0; while(a[i]!='\0') printf("%c",a[i++]); } 

This is useful?

0
source

Source: https://habr.com/ru/post/1441016/


All Articles