Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conflicting types in C

Tags:

c

I was trying to build a very simple program in C that returns a float value from a function, but for some reason I got an error.

#include<stdio.h>
int main(){
    double returning;
    returning = regre();
    printf("%f", returning);
    return 0;
}
double regre(){
    double re = 14.35;
    return re;
}

The error am getting says:

conflicting types for 'regre'

previous implicit declaration of regre was here

like image 805
karlelizabeth Avatar asked Oct 13 '12 03:10

karlelizabeth


2 Answers

That error message is telling you exactly what's happening - there is an implicit declaration of regre because you don't define it until after main(). Just add a forward declaration:

double regre();

Before main(), or just move the whole function up there.

like image 196
Carl Norum Avatar answered Sep 28 '22 01:09

Carl Norum


previous implicit declaration of `regre` was here

If a function is unknown, then compiler considers it as int functionname() by default. In your case int regre() will be declared here.

conflicting types for 'regre' 

When your actual function double regre() is noticed, this conflict error occurs. To solve this issue, double regre() function should be declared before it's actual use.

#include<stdio.h>
double regre(); //Forward Declaration
int main(){
    double returning;
    returning = regre();
    printf("%f", returning);
    return 0;
}
double regre(){
    double re = 14.35;
    return re;
}

For more info on Forward Declaration, refer the following link.

http://en.wikipedia.org/wiki/Forward_declaration

like image 41
Jeyaram Avatar answered Sep 28 '22 01:09

Jeyaram