Home » C Popular & Interesting Questions

What is 'Conflicting Types for' Error in C?

Conflicting Types for Error in C programming

Conflicting Types for Error - Is a common mistake in programming it occurs due to incompatibility of parameters/arguments type in declaration and definition of the function.

Let’s understand by example:

In this program we are finding the average of two numbers and we create a user define function name average().

#include <stdio.h>
 
/*function declaration*/
float average(int, int);
 
int main()
{
    printf("Average is: %f\n",average(36.24,24.36));
 
    return 0;
}
/*function definition*/
float average(float x, float y)
{
    return ((x+y)/2);
}

Output

    error: conflicting types for 'average'

Here compiler returned an error conflicting types for 'average'.

What is the wrong in this program?

See the declaration of the function average(): float average(int, int);

Here type of arguments are int, int while at the time of defining the function argument types are float, float. float average(float x, float y)

So types of arguments are different in declaration and definition, argument types are not same.


How to correct the error?

Argument types should be in declaration and definition.

Declaration also should be:
float average(float x, float y);

Program after fixing the problem

#include <stdio.h>
 
/*function declaration*/
float average(float, float);
 
int main()
{
    printf("Average is: %f\n",average(36.24,24.36));
 
    return 0;
}
 
/*function definition*/
float average(float x, float y)
{
    return ((x+y)/2);
}

Output

    Average is: 30.300001






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.