#include <stdio.h>
#include <math.h>
#define MAX_BITS 32
int counter = 0, dec_eqv = 0;
int runcounter = 1;
int binToDec(char *bit)
{
printf("dec_eqv is %d\n", dec_eqv);
if (counter == 0 && runcounter)
{
dec_eqv = 0;
int i = 0;
while (*(bit + i++))
;
counter = (i - 1) - 1;
runcounter = 0;
printf("counter is %d\n", counter);
}
if (*bit != 0)
{
printf("*bit not zero\n");
if (*bit == '1')
{
dec_eqv += (int)pow(2, counter--);
}
else
{
counter--;
}
binToDec(bit + 1);
}
else
{
printf("here *bit is 0\n");
runcounter = 1;
printf ("here dec_eqv is %d\n", dec_eqv);
return dec_eqv;
printf ("Skipping return\n");
}
return -1;
}
int main()
{
char bin[MAX_BITS];
printf("Enter binary number (%d-bit max): ", MAX_BITS);
scanf("%[^\n]s", bin);
int result = binToDec(bin);
printf("Decimal equivalent of 0b%s is %d.", bin, result);
return 0;
}
I've added the printf() statements in binToDec() for debugging, and it seems that the compiler is ignoring the return dec_eqv; statement in the final else block and directly executing the return -1; at the end. What could be possibly wrong? I am using Clang/LLVM compiler in Ubuntu 23.10 AMD64 and no errors or warnings were generated during compilation.