C K&R 1.7 power function -


i'm teaching c k&r book, , got confused power function 1.7 example.

well, when wrote code example given book on code::block , ran it, error occurred: undefined reference 'power'.

the codes follow:

#include <stdio.h> #include <stdlib.h>  int power(int m, int n); main () {     int i;     (i = 0; < 10; ++i)         printf("%d %d %d\n", i, power(2, i), power(-3, i));     return 0; } 

is power function predefined function provided library? because program above didn't define body part of power

if so, why did run error? did include wrong library?

the error message says function power not defined. sure somewhere in book there definition of function or there exercise requires write function yourself.

it can written simply. example positive n function can like

int power(int m, int n) {     int result = 1;      ( ; n; --n ) result *= m;      return result; } 

you can modify function such way accept negative n.:)

take account better if function had return type long long int

for example

long long int power(int m, unsigned int n) {     long long int result = 1;      ( ; n; --n ) result *= m;      return result; } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -