-3

I wrote a C program that converts each base to another one..but when I'm going to convert ascii code to number I face a problem... please help :) I think my problem is because I cant convert assci to number and it says: ====> undefined reference to `pow'

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

int CB, DB;
 void base(void)
 {
   int adad2[100], i=-1,j;
   char adad1[100], ch;
   long int num1=0, num2=0;
   printf("Enter your num: ");
   scanf("%c", &ch);

   do
     {
       i++;
       scanf("%c", &adad1[i]);
     } while(adad1[i]!='\n');

   j=i-1;
   for(i=j;i>=0;i--)
     { //converts the base to 10.                                                                                                                                 
       if(adad1[i]<='9'&& adad1[i]>='0')
     {
       num1+=((long int)pow((float)CB,(j-i)))*(((int)adad1[i])-48);  //converting ascii code to num                                         
     }
       else if(adad1[i]<='Z'&&adad1[i]>='A')
     {
       num1+=((long int)pow((float)CB,(j-i)))*(((int)adad1[i])-55);
     }
       else if(adad1[i]<='z'&&adad1[i]>='a')
     {
       num1+=((long int)pow((float)CB,(j-i)))*(((int)adad1[i])-87);
     }
     }

   i=0;
   while(num1>=DB)
     { //converts the base to b. (START)                                                                                                                             
       adad2[i]=num1%DB;
       i++;
       num1/=DB;
     }

   adad2[i]=num1; //converts the base to b. (END)                                                                                                                                
   printf("\nResult: \n");

   for(;i>=0;i--)
     { //prints the result.                                                                                                                                          
       if(adad2[i]<=9&&adad2[i]>=0){
     printf("%d",adad2[i]);
       }

       else if(adad2[i]>=10&&adad2[i]<=35){
     printf("%c",(char)(adad2[i]+55));
       }
     }
 }

 void main(void)
 {
   printf("\nEnter current base: ");
   scanf("%d", &CB);

   printf("\nEnter desired base: ");
   scanf("%d", &DB);

   base();

 }
MLSC
  • 5,872
  • 8
  • 55
  • 89
  • If you are using `gcc` to include `math.h` compile with `-lm` flag – Grijesh Chauhan Dec 01 '13 at 07:28
  • Welcome to SO. Please get your question straight before asking here. It seems that your actual question title has nothing to do with the problem you are facing. You are mixing up your objective (base conversion) and the error that you are facing. At SO we are interested to help you with the later. Once you have that cleared up for yourself, you should have been able to find a similar question with a valid answer all by yourself. – Jens Gustedt Dec 01 '13 at 08:48

3 Answers3

1

When you are getting the error from your compiler

 undefined reference to `pow'  

then it means that the library function is not linked with your program. For this you must have to include the header that contains the definition of pow function. In GCC compile it with using lm flag. It will include <math.h> header which contains the definition of the library function pow.

haccks
  • 104,019
  • 25
  • 176
  • 264
0

If you are using gcc, you might need to compile like this:

gcc yourfile.c -o yourapp -lm

The lm option links the math library.

On a side note, do not use void main instead use something standard like:

int main(int argc, char *argv[])

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

As others already pointed out, you can "resolve" the linker error by linking against the math library. However, this is not your primary problem.

Floating-point numbers are inherently inexact, and thus it is A Very Bad Idea (TM) to use them with supposedly exact integer operations. If a call to pow() returns a slightly smaller number than the expected power of the base, then, due to truncation, you will get erroneous results.

I suggest you write an integer power function yourself that can be used safely; e. g.:

unsigned long integer_pow(unsigned long base, unsigned long exp)
{
    unsigned long res = 1;
    while (exp--) {
        res *= base;
    }

    return res;
}