5

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?

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
mayank raj
  • 59
  • 3
  • As the two answers I see note, you have an unnecessary call to `pow()` in that code. I don't think the answers mention that you have an unnecessary call to `fun()` in your first program. Note that if you enter a number that is 13 or larger, your factorial code will suffer from a signed integer overflow, leading to undefined behaviour. – Jonathan Leffler Dec 31 '19 at 23:23
  • @JonathanLeffler Mine does. – S.S. Anne Jan 01 '20 at 00:58
  • I see mention of the unnecessary call to `pow()` and recognized that both answers mention that. I still don’t see the reference to `fun()` by name, but on a re-re-read, I do see a contextual reference to it, which is why I missed it before. – Jonathan Leffler Jan 01 '20 at 01:05

2 Answers2

5

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;
}
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Nick Reed
  • 4,989
  • 4
  • 17
  • 37
  • 1
    You might note that there's no need for the first call to `powr`, and that having a function prototype is required for everything but C99. – S.S. Anne Dec 31 '19 at 19:21
  • 3
    It might be the problem. The C standard allows for implicit inclusion. – Bathsheba Dec 31 '19 at 19:21
  • @Bathsheba: C++ allows standard headers to include other standard headers. The C standard does not. That said, gcc does have a built-in named `pow()` and what happens on my simple tests is that the code gets compiled using the built-in's prototype (`double pow(double, double)`) but the function defined in the source file is what gets called (incorrectly). The general advice of declaring functions and objects before calling them is always good. – Michael Burr Dec 31 '19 at 20:08
3

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));
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76