×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

What is 'Conflicting Types for' Error in C?

'Conflicting Types for' Error in C

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.

Example of Conflicting Types for Error

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 Fix Conflicting Types for 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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.