0

I am creating an simple program in which I am using long long int but I got compiler error. Please help me to resolve this error.

Error] conflicting types for countTrees

I got error on this line

long long int countTrees(long long int numKeys)

Here is my code:

#include <stdio.h>
#include <math.h>

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        long long int n;
        scanf("%lld", &n);
        long long int result = countTrees(n);
        printf("%lld\n",result);        
    }

return 0;
}  
long long int countTrees(long long int numKeys) {

  if (numKeys <=1) {
    return(1);
  }
  else {
    long long int sum = 0;
    long long int left,right,root;

    for (root=1; root<=numKeys; root++) {
      left = countTrees(root - 1);
      right = countTrees(numKeys - root);

      sum += left*right;
    }

    return(sum);
  }
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Saket Mittal
  • 3,726
  • 3
  • 29
  • 49

1 Answers1

3

You get the error, because of the implicit declaration of the countTrees() not matching the actual definition.

To clarify "implicit declaration" when a function is called, but the compiler has not seen the function definition yet, it assumes that the function returns int and accepts any number (and type) of parameters.

In C99 and future standards, the implicit declaration is made invalid, so a compiler should complain if a function, which is not yet defined (or at least. prototyped, through a forward declaration), is used (called).

To resolve the issue, you can do either

  • add a forward declaration of your function.
  • define the function before main().
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261