-1

I keep getting the warning that my function has an undefined reference and that doesn't really say much to me or how to fix it. Here are the errors

log_2.c: In function ‘main’: log_2.c:29: warning: implicit declaration of function ‘logbase2’ /tmp/ccAXAmVb.o: In function 'main': log_2.c:(.text+0x5e): undefined reference to `logbase2' collect2: ld returned 1 exit status

Heres my code:

int logbasetwo (int number)
{
int test;
for (int i = 0; i< number; i++){
    test = 2 ^ i;
    int result = i;
}


return result;
}

int main(){

printf("Enter a positive integer: ");
int number = get_int();
int logresult;
if (number > 0){
logresult=logbase2(number);

}


else (number < 0){
    printf("Not a positive number. Re-enter: ");
    number = get_int(); 

}

printf("Log base two of number is:%i", logresult);


}


return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
user3457828
  • 23
  • 1
  • 4

1 Answers1

4

Well, in your code , both logbase2() and logbasetwo() are used, which are not the same !!!

You have defined a function named logbasetwo(), but you called logbase2().

Change either of them to match other one.

Also, you need to change the logic test = 2 ^ i;. As mentioned in earlier comment by Mr. @Bathsheba, ^ operator is for XOR, not exponentiation.

You need to use pow().

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