Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my factorial program working but my almost identical pow program is not working?

Here is my factorial program—this is executing and giving a correct result:

#include <stdio.h>

int main()
{
    int n;

    printf("enter the no=");
    scanf("%d", &n);
    fun(n);
    printf("%d\n", fun(n));

    return 0;
}

int fun(int n)
{
    if(n == 0)
        return 1;
    else
        return fun(n - 1) * n;
}

This is my program to compute the power of a number—this is giving 0 instead of the correct result and yet is almost identical:

#include <stdio.h>

int main()
{
    int m, n;

    printf("enter the no=");
    scanf("%d%d", &m, &n);
    pow(m, n);
    printf("%d\n", pow(m, n));

    return 0;
}
int pow(int m, int n)
{
    if(n == 0)
        return 1;
    else
        return pow(m, n - 1) * m;
}

Both are running on same compiler.

Why is my factorial program working but my almost identical power program is not working?

like image 556
mayank raj Avatar asked Dec 31 '19 19:12

mayank raj


2 Answers

A few issues are present here. First and foremost, you didn't declare a prototype for your function before calling it the first time. To do so, you need to place int pow(int, int); above main. This lets the compiler know exactly what your function expects and what it returns.

Ordinarily, this wouldn't cause the behavior you're seeing (though it is bad practice), but there's also already a function named pow in the C library. Since you never gave it a definition of your own, it's being implicitly included in your code. Now, it's expecting you to put two doubles in and get a double out.

Add the prototype at the top and rename your function, and you'll fix both of these issues at once.

Demo

(Also, for what it's worth, you've got an unnecessary call.)

#include <stdio.h>

int powr(int, int);    // helps avoid compiler warnings

int main()
{
    int m, n;

    printf("enter the no=");
    scanf("%d%d", &m, &n);
    powr(m, n);             // unnecessary
    printf("%d\n", powr(m, n));

    return 0;
}

int powr(int m, int n)
{
    if(n == 0)
        return 1;
    else
        return powr(m, n - 1) * m;
}
like image 164
Nick Reed Avatar answered Nov 16 '22 16:11

Nick Reed


For a little backstory behind this: The GNU C compiler (presumably what you're using) has implicit declarations for most of the Standard C Library functions that can be optimized in target-specific ways. pow is one of them.

To fix this, you should rename your pow function to something not reserved by the Standard C library and provide a prototype for it, like so:

#include <stdio.h>

int power(int m, int n);

If you compile in strict C89/C90 compliance mode, you don't even need to provide a prototype due to implicit function declaration rules. However, if you compile with any other standard (which is the default and highly recommended), you'll need to provide a prototype for that function, as shown above.

I'd also like to note that you have an unnecessary call to your power-computing program (also present in the factorial-computing program):

scanf("%d%d", &m, &n);
power(m, n); // here
printf("%d\n", power(m, n));
like image 23
S.S. Anne Avatar answered Nov 16 '22 16:11

S.S. Anne