Reputation: 707
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!
Upvotes: 4
Views: 24580
Reputation: 6684
Whatever you are doing in the code posted, you are not using double
datatype at all, within the function. And from the function return type, it appears 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 your compiler error, you may not get the desired results always, due to such dangerous usage of datatype in the function.
To know why its 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 that function body.
And the error "conflicting types for 'hexRepresentation'" will be shown when there exists a prior declaration or definition of the same named function hexRepresentation
before this function definition. Also, if you do not declare a function and it only appears after being called, it is automatically assumed to be int
by the compiler.
So, declare and define your function before main() or declare before main() and define the function anywhere else 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()
{
...
}
Upvotes: 1
Reputation: 211
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));
}
Upvotes: 9
Reputation: 9234
Just create one stack ( for char) which has push and pop functions. I am not returning the value just printing it there in the same function. (have implemented for integral values only)
#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++]);
}
Is it helpful ?
Upvotes: 0