Program infinitely runs in C, no errors during compilation or run time

This is one of my first programs in C, so please bear with me! I wrote this code to calculate a base number raised to another given number. I got no compilation errors, except when I run my code, nothing happens. What am I doing wrong?

Thank you!

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

int expCalculator(int base, int exponent) {
    if (exponent == 0){
        return 1;
    }
    else if (exponent % 2) {
        return base * expCalculator(base, exponent - 1);
    }
    else {
        int temp = expCalculator(base, exponent / 2);
        return temp * temp;
    }
}

int main() {
    float base, answer;
    int exponent;
    int positiveBase;
    char buffer[10];

    positiveBase = 0;
    while (positiveBase == 0){
        printf("Enter a base number: ");
        scanf(" %f", &base);
        if (base > 0){
            positiveBase = 1;
            printf("Please enter an exponent value to raise the base to: ");
            scanf(" %d", &exponent);
            answer = expCalculator(base, abs(exponent));
            gcvt(base, 10, buffer);
            printf(buffer, " to the power of ", exponent, " is ", answer);
        }
        else {
          printf("Please enter a positive base! Try again.");
        }
    }
    return 0;
}

4 answers

  • answered 2018-10-11 19:46 shirish

    • I think the mistake in the code is the printf statement. The first argument to printf statement is a string which is the format specifier and then followed by the arguments.
    • You might have got confused on reading the manual of printf function's prototype
      int printf(const char *format, ...); as the first argument is const char* which doesn't mean you can pass the buffer which is not const
    • There is no need to convert the numbers into null terminated strings as printf has format specifiers like %f, %d, etc to take care of printing numbers.


    #include <stdio.h>
    #include <stdlib.h>
    
    float expCalculator(float base, int exponent) {// Need to change the types to float
      if (exponent == 0) {
        return 1;
      } else if (exponent % 2) {
        return base * expCalculator(base, exponent - 1);
      } else {
        float temp = expCalculator(base, exponent / 2); // One more type change
        return temp * temp;
      }
    }
    
    int main() {
      float base, answer;
      int exponent;
      int positiveBase;
      //char buffer[10]; No need of a buffer
    
      positiveBase = 0;
      while (positiveBase == 0) {
        printf("Enter a base number: ");
        scanf("%f", &base);
        if (base > 0) {
          positiveBase = 1;
          printf("Please enter an exponent value to raise the base to: ");
          scanf("%d", &exponent);
          answer = expCalculator(base, abs(exponent));
          // Statement below is the major change you need to make
          printf("Base %f to the exponent %d = %f\n", base, exponent, answer);
        } else {
          printf("Please enter a positive base! Try again.");
        }
      }
      return 0;
    }
    



    Output:
    Enter a base number: 2.3
    Please enter an exponent value to raise the base to: 2
    Base 2.300000 to the exponent 2 = 5.290000

  • answered 2018-10-11 19:52 dbush

    You're not printing the results correctly:

    printf(buffer, " to the power of ", exponent, " is ", answer);
    

    The first parameter to printf is a format string, and the following parameters are the values that fit the format string. The compiler won't throw any warnings in this case because the first argument is of the correct type and the rest are variable arguments.

    Many compilers will check these parameters against the given format string, but that doesn't happen in this case because the format string isn't a string constant. The only thing that gets printed is buffer, which is base converted to a string.

    What you want is:

    printf("%.10f to the power of %d is %f\n", base, exponent, answer);
    

    Note that this prints base directly using a format string, as the gcvt function is obsolete.

    As for why you're not seeing anything in your terminal, it could be due to buffering. The prompt you print doesn't contain a newline, so the output buffer won't necessarily get flushed. You'll need to do so manually:

    printf("Please enter an exponent value to raise the base to: ");
    fflush(stdout);
    

  • answered 2018-10-11 20:03 PC Freak

    Replace your printf() function line with : printf("%3.2f to the power of %d is %3.2f", base, exponent, answer);

    The reason is first you need to provide the format of the string and then the values in the further arguments. Refer to the following link for more detailed instruction. http://www.cplusplus.com/reference/cstdio/printf/

  • answered 2018-10-11 20:34 chqrlie

    There are multiple problems in your code:

    • negative base values pose no problem, why refuse them?
    • you did not include the standard header files for the library functions used.
    • function expCalculator() should use floating point arithmetics, preferably double.
    • negative exponents should be handled, it is rather easy.
    • you should not use gcvt: this function is non standard and might not be supported on some systems. Instead use printf with an appropriate format string.
    • the output statement printf(buffer, " to the power of ", exponent, " is ", answer); is incorrect. You should instead use a format string and arguments as show below.

    Here is a corrected version

    #include <stdio.h>
    #include <stdlib.h>
    
    double expCalculator(double base, int exponent) {
        if (exponent < 0) {
            return 1 / expCalculator(base, -exponent);
        } else
        if (exponent == 0) {
            return 1;
        } else
        if (exponent % 2) {
            return base * expCalculator(base, exponent - 1);
        } else {
            double temp = expCalculator(base, exponent / 2);
            return temp * temp;
        }
    }
    
    int main() {
        double base, answer;
        int exponent;
        int positiveBase;
    
        positiveBase = 0;
        while (positiveBase == 0) {
            printf("Enter a base number: ");
            scanf("%lf", &base);
            if (base > 0){
                positiveBase = 1;
                printf("Please enter an exponent value to raise the base to: ");
                scanf("%d", &exponent);
                answer = expCalculator(base, exponent);
                printf("%g to the power of %d is %g\n", base, exponent, answer);
            } else {
                printf("Please enter a positive base! Try again.\n");
            }
        }
        return 0;
    }