-1

why is it showing errors like ld returned 1 exit status and undefined reference to `powr(int, int)'

#include<stdio.h>

int powr (int m , int n );
int main (){

     int i,m,n;
     printf("print the base\n");
     scanf("%d",&m);
     printf("print the expoenent\n");
     scanf("%d",&n);

     int p ;
     if (n == 1 ){
        return m;
     }
     else {
        p = powr(m,n/2);
        if (n%2 ==0 ){
            return p*p ;

         }
         else {
            return p*p*m;
         }
    }
}
haccks
  • 104,019
  • 25
  • 176
  • 264
manish
  • 1
  • Possible duplicate of [C - undefined reference to sqrt (or other mathematical functions)](http://stackoverflow.com/questions/5248919/c-undefined-reference-to-sqrt-or-other-mathematical-functions) – Box Box Box Box Mar 01 '16 at 09:22
  • 3
    There ain't no standard function `powr(int, int)` in C. Did you mean `pow(double, double)` from `math.h`? – Bathsheba Mar 01 '16 at 09:23
  • You put a prototype of `powr` function but you mentioned no where its definition in the question. Can't say anything without seeing the definition. – haccks Mar 01 '16 at 09:38

2 Answers2

2

I think you want to use the pow function which returns the power of two numbers.

Because pow is in math.h. So include it:

#include <math.h>

And while compiling it, link math.h:

gcc program.c -lm

-lm is linking it to the math.h library.

Also see: C - undefined reference to sqrt (or other mathematical functions)

Community
  • 1
  • 1
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
0

Its because you have to define your function powr in your code just declaring it will not help.

Soumyadip
  • 7
  • 3