1

I am a totally newbie about C programming. so my program is very long, sorry.

my professor wants to have a number system- binary to decimal, decimal to binary, octal to decimal, hexadecimal to binary. he also want to have a loop( if he wants to exit press [E], if not then press any key). Now i'm having a problem with this hexadecimal because it keeps saying " type mismatch in redeclaration" and i don't know now how to solve this problem.

so heres my not yet finished program because of "hexadecimal" problem. help me with this error. don't mind the octal to decimal, I am currently programming it :)

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 1000

long num, decimal(long), octal(long), binary(long),j;
char hexadecimal(char), k[MAX];

main()
{
char choice;
clrscr();

printf("[B]inary to Decimal\n");
printf("[D]ecimal to Binary\n");
printf("[O]ctal to Decimal\n");
printf("[H]exadecimal to Binary\n");
printf("[E]xit\n");
printf(" Enter your choice....");

choice=getche();
switch(choice)
{
case 'b':
case 'B': binary(j); break;
case 'd':
case 'D': decimal(num); break;
case 'o':
case 'O': 
case 'h':
case 'H': hexadecimal(k[MAX]); break;
case 'e':
case 'E': return 0;
default: printf("\n Invalid choice.... press any key to REPEAT");
getch();
main();
}
printf("\nDo you want to [E]xit?");
choice=getch();
switch(choice)
{
case 'e':
case 'E': printf("\nInvalid choice... press any key to repeat");
getch();
main();
}
getch();
return 0;
}

long binary(long j)
{
 long binary_val,decimal_val=0, base=1, rem;
 printf("Enter a binary number( 1s & 0s): ");
 scanf("%ld",&j);
 binary_val=j;
 while(j>0)
  {
    rem=j % 10;
    decimal_val=decimal_val + rem * base;
    j= j/ 10;
    base=base * 2;
  }
 printf(" The Binary Number is %ld\n",binary_val);
 printf(" Its decimal equivalent is = %d\n",decimal_val);
}

long decimal(long num)
{
long decimal_num, remainder, base=1, binary=0;
printf(" \nEnter a decimal integer: ");
scanf("%ld",&num);
decimal_num=num;

while(num>0)
  {
     remainder= num % 2;
     binary=binary + remainder * base;
     num=num/2;
     base= base * 10;
  }
printf(" Input number is %d\n",decimal_num);
printf(" Its binary equivalent is = %ld",binary);
}

 char hexadecimal(char k[MAX])
{
long int i=0;
clrscr();

printf(" Enter any Hexadecimal number:  ");
scanf("%s",&k);
printf("\n Equivalent binary value: ");

while(k[i])
{
  switch(k[i])
  {
    case '0': printf("0000"); break;
    case '1': printf("0001"); break;
    case '2': printf("0010"); break;
    case '3': printf("0011"); break;
    case '4': printf("0100"); break;
    case '5': printf("0101"); break;
    case '6': printf("0110"); break;
    case '7': printf("0111"); break;
    case '8': printf("1000"); break;
    case '9': printf("1001"); break;
    case 'a':
    case 'A': printf("1010"); break;
    case 'b':
    case 'B': printf("1011"); break;
    case 'c':
    case 'C': printf("1100"); break;
    case 'd':
    case 'D': printf("1101"); break;
    case 'e':
    case 'E': printf("1110"); break;
    case 'f':
    case 'F': printf("1111"); break;
    default: printf("\n Invalid hexadecimal digit %c",k[i]); return 0;
   }
   i++; 
  } 
}
Spencer
  • 9
  • 6

1 Answers1

3

The error you are getting type mismatch in redeclaration of hexadecimalis a result of the difference between the function you prototyped and implemented.

Your prototype is:

char hexadecimal(char), k[MAX];

This line prototypes a function hexadecimal that returns a char and takes a char as an argument AND this line also declares a global char array k of size MAX.

Your actual function is:

char hexadecimal(char k[MAX])

This function is a function that returns a char, but instead of taking a char like your prototype it instead takes a char array of size MAX. As you can see the prototyped function and the function itself are not the same. By making the functions exactly the same you will fix your issue.

To be honest, you don't need to pass anything into that function nor make a global char array as you can locally hold the array based on your code. The only other time you use the array you just pass it to this function which means it is better of as a local to that function anyway. So, you can simply do this:

char hexadecimal(void)
{
    char k[MAX]
    //same code below...

Now the function takes no arguments and k is still declared in the function, but is local instead of global. The prototype for this function would simply be:

char hexadecimal(void);
Dom
  • 1,687
  • 6
  • 27
  • 37