0

I'm teaching my C with K&R this book, and got confused by the power function from 1.7 example.

Well, when I wrote the code exactly from the example given by the book on Code::Block and ran it, an error occurred: undefined reference to 'power'.

The codes are as follow:

#include <stdio.h>
#include <stdlib.h>

int power(int m, int n);
main ()
{
    int i;
    for (i = 0; i < 10; ++i)
        printf("%d %d %d\n", i, power(2, i), power(-3, i));
    return 0;
}

Is power function a predefined function provided by library? Because program above didn't define the body part of power

If so, why did I run into error? Did I include the wrong library?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
steveluoxin
  • 419
  • 4
  • 14
  • 1
    Either change `power` to `pow`(and remove `int power(int m, int n);` if you are going to do this) or create your own version of it in a function named `power` that takes in two `int`s and returns an `int` – Spikatrix Apr 25 '15 at 08:30
  • 2
    @steveluoxin I am sure that somewhere in the book there is the definition of power or there is an exercise that requires that you write the function yourself.:) Check the book more attentively. – Vlad from Moscow Apr 25 '15 at 08:35
  • Signature before `int power(int m, int n);` indicated that it is user defined function. – haccks Apr 25 '15 at 08:35
  • http://stackoverflow.com/questions/8827170/section-1-8-of-k-r-cant-figure-out-why-line-int-powerint-m-int-n-is-i – Retired Ninja Apr 25 '15 at 08:36
  • The full code of the `power` function is given at the top of the next page. – user3386109 Apr 25 '15 at 08:44
  • @CoolGuy Thanks, man. I didn't realize the second part from example actually defines the _power_ function – steveluoxin Apr 25 '15 at 08:46
  • Thanks to all, I didn't write the function definition part which is presented by k&r book – steveluoxin Apr 25 '15 at 08:50

3 Answers3

2

The error message says that function power is not defined. I am sure that somewhere in the book there is the definition of the function or there is an exercise that requires that you write the function yourself.

It can be written simply. For example for positive n the function can look like

int power(int m, int n)
{
    int result = 1;

    for ( ; n; --n ) result *= m;

    return result;
}

You can modify the function such a way that it would accept negative n.:)

Take into account that it would be much better if the function had return type long long int

For example

long long int power(int m, unsigned int n)
{
    long long int result = 1;

    for ( ; n; --n ) result *= m;

    return result;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    With all due respect sir, isn't it like doing his/her homework and encouraging for asking more such questions in future? IMHO, we still don't have any proof of real effort from OP's side. – Sourav Ghosh Apr 25 '15 at 08:42
  • 1
    @Sourav Ghosh In fact it is yours or my homework.:) You can either do it for yourself or simply ignore it.:) Everybody decides himself.:) – Vlad from Moscow Apr 25 '15 at 08:44
0

There is nothing called power() defined in standard C library. If you want a power() function, you have to write your own before using it.

If you want a library function, it is called pow(), defined in math library. Include the header file math.h, without having need to forward define the prototype. Also, don't forget to link against the math library using -lm.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

Is power function a predefined function provided by library?

Answer: No. Power function is not predefined.

"Because program above didn't define the body part of power"

I made the same mistake by not checking the next page. See next page,you will find its(power function) definition.

Complete code-

#include <stdio.h>

int power(int m, int n);
/* test power function */
int main(){
    
    int i;
    for(i = 0; i < 10; ++i)
        printf("%d %2d %3d", i, power(2,i), power(-3,i));
    return 0;
    }


int power(int base, int n)
{
    int i, p;
    p=1;

    for(i=1;i<=n;++i)
        p=p*base;

    return p;
}

About error(s):

In my case i tried exactly the same code(except this line#include <stdlib.h>) as yours in geany editor and got-

gcc -Wall -o "pfunc" "pfunc.c" (in directory: /home/anz)
Compilation finished successfully.

if i try to compile from terminal with cc pfunc.c i get error(power undefined)

May be compiler issue? Anyway, im trying know the exact reason. Once i know i will edit.

Tanzin
  • 71
  • 2
  • 5