Why do I get clang: error: linker command failed with exit code 1?
You just declared the function. There is not any definition in code. At the time of linking process , compiler(here clang) cannot link power
function to its definition so linker throws the error in this kind of situation. If you define
int power(int x, int y) { \*do calculation*/ }
Then linker can link your declaration of power
function to its definition ,you will not get any error.
For integer number I have made function for you.
#include <stdio.h> int power(int base, int exp); int main() { int i; for (i=0; i<10; ++i) printf("%d %d %d\n", i, power(2,i), power(-3,i)); return 0; } int power(int base, int exp) { int result = 1; while (exp) { if (exp & 1) result *= base; exp >>= 1; base *= base; } return result; }
Compile this with gcc file.c
Hope you understand the function. Good luck 🙂