C Programming

Getting “conflicting types for function” in C, why?

Every programming language has some predefined data types that define the kind of data that can be provided in variables. In C, for instance, data types include integers, floats, doubles, characters, and pointers. Variables, arrays, and functions may all be defined using these data types, among other things. Therefore, a function’s data type represents the type of data that the function would return.

In C programming language, a common error message that developers come across is the “conflicting types for function” error. This error message arises when the compiler detects two different types of data associated with a single function. This article discusses what causes this error and how to address it.

“Conflicting Types for Function” Error

When we declare a function in C, it must match the type of the function definition. For example, if we define a function to return an integer, the declaration must also define the function to return an integer. If the types of the definition and the declaration do not match, the “conflicting types for function” error is thrown by the C compiler.

Reasons Why This Error Occurs

There are several reasons why conflicting types for functions can occur.

1: Mismatched Data Types

The first reason is that data types may be mismatched between the function call and the function definition. For example, if we define a function to return a float and then try to call a function with the expectation of an integer, the compiler will detect a conflict in types.

2: Function Prototype Defined Wrongly

Another reason for the conflict is that the function prototype is not defined or declared correctly. The kinds of parameters a function receives and the types of values it returns are declared in a function prototype. If the prototype does not match the definition, the error would be thrown.

3: Typing Error

Finally, the “conflicting types for function” error message can also occur due to a simple typing error, such as a misspelled function name, a missing semicolon, or a missing closing parenthesis. Therefore, it is important to check for syntax errors when we get this error message.

Here is the sample code that generates the “conflicting type for function” error in C.

#include <stdio.h>

float average(int, int);
int main()
      {
printf("Average is: %f\n",average(27.32,14.53));
           return 0;
      }
float average(float x, float y)
      {
           return ((x+y)/2);
      }

In the above code, the type of arguments in the declaration of function ‘average()’ is int, while at the time of defining the function average, we are using the data types float for function arguments. As types of function parameters are different for declaring and defining the same function, we get the error “conflicting types for ‘average’”.

Output

Corrected Code

To correct the error, we need to check that the argument types in declaring and defining the functions are the same.

#include <stdio.h>

float average(float, float);
int main()
       {
printf("Average is: %f\n",average(27.32,14.53));
            return 0;
      }
float average(float x, float y)
     {
            return ((x+y)/2);
     }

In the above code, we are using the same types of parameters for both, the definition, and the declaration of the function. Hence it gives us the output, i.e., the average of both values.

Output

Conclusion

“Conflicting types for function” error message in C appears when there is a mismatch between the function prototype and its definition, as well as when the type of value returned by the function is not consistent between the two. To resolve this error, we must ensure that both the function prototype and its definition are consistent in terms of the number, order, and types of arguments accepted and the type of value returned. We must also check for simple syntax errors when we encounter this error message to avoid unnecessary frustration.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.