简体   繁体   中英

Conflicting types error in the function declaration of a division with floating point function

This was supposed to be a recursive function that keeps adding to 'h' the values of 1/i while i is less than 'h', but i keep receiving conflicting types error in the function declaration, what im doing wrong? Its just happening with float.

#include <stdio.h>

int main()
{
    int i=0, n = 5;
    float h;
    division(i, n, h);
    return 0;
}

float division(int i, int n, float h)
{
    if(i<n)
    {
        h += 1/i;
        division(i, n, h);
    }
    else
    {
        printf("the sum is: %f",h);
        return h;
    }
}

The problem giving rise to a "conflicting types error" is that there is no declaration of function division() in scope at the point where main() attempts to call it. Such a function call is non-conforming since C99, but many compilers provide an implicit declaration for use in such a function call, because this was the behavior of primordial C, and supported through C90.

HOWEVER, the implicit declaration used in this case would be equivalent to

int division(int i, int n, double h);

, which does not match and is not compatible with the actual function signature. When the compiler later discovers that, it emits a diagnostic to warn you about it. Possibly it rejects the code for that reason -- that's not clear from the question.

To resolve the situation, you must declare the function before its (first) use. One way to do that would be to move the whole definition of division before main() , but you can also just put a forward declaration of the function before main() . That would look similar to the above (wrong) declaration, but it would correctly reflect the function's true return type and parameter types:

float division(int i, int n, float h);
  • The function
float division(int i, int n, float h)

Is used used before it's declared, you should place a declaration before main, or simply move it to before main. This is the reason for the error, since the function is implicitly declared, the return type defaults to int conflicting with the definition.

A couple of other issues:

  • In that same function, a return is not always guaranteed.

  • In the line

h += 1/i;

You have a potencial division by 0, which leads to undefined behaviour .

  • h is used unintialized, as the value of h is not known adding to it also invokes undefined behaviour.

As advised by other users, some of these problems would become apparent with compiler warnings on.

Declare the function before you use it. Either put the function definition before main or insert the declaration float division(int i, int n, float h); before main.

This is required in modern C, but some compilers will assume a default declaration if you do not provide one. The default declaration has a return type of int , which conflicts with the actual return type of your function, float . That causes the error message.

Turn on warning messages for your compiler and pay attention to them.

Another problem beside "conflicting types" : infinite regression

If i < n , then that will be true in the recursive call.

float division(int i, int n, float h) {
    if(i<n) {
        h += 1/i;  // Note: integer division, like should be 1.0f/i;
        division(i, n, h);

Perhaps OP wants the below. I am not sure though, but something that changes i .

        division(i+1, n, h);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM