Asdemuertes
Asdemuertes

Reputation: 323

Conflicting type C

I'm having some problem with a conflicting type that I don't understand. I want to change a date previously set but it's giving me a headache.

int main(){
float any=11;
llegirany(&any);
printf("%f",any);
getch();    
}

void llegirany(float * any){    
float auxi; 
auxi=llegirinterval(1,31);  
*any= auxi;
}

float llegirinterval(float n1,float n2){
float aux;
       do{          
            scanf("%f",&aux);
        }while(aux>n2 || aux<n1);
return aux;
}

And I get this output error:

65 7 D:\DAM\PRo\pruebaconio.c [Error] conflicting types for 'llegirinterval' 62 7 D:\DAM\PRo\pruebaconio.c [Note] previous implicit declaration of 'llegirinterval' was here

Can somebody help me please?

Upvotes: 0

Views: 227

Answers (2)

xabitrigo
xabitrigo

Reputation: 1431

You are using the function llegirinterval before declaring it.

You should move the definition of llegirinterval before the definition of llegirany or at least declare llegirinterval before it's used.

Check the difference between definition and declaration

EDITED following @Olaf comment.

Upvotes: 3

Jay Elston
Jay Elston

Reputation: 2068

Try adding a declaration to the function llegirinterval() before you use it. You should also declare other functions contained in the code as well:

void llegirany(float *any);
float llegirinterval(float n1,float n2);

int main(){
    float any=11;

...

void llegirany(float * any){
    float auxi;
    auxi=llegirinterval(1,31);
    *any= auxi;
}

By default, C considers the type of any variable name that has not been explicitly typed yet as int. So, when it sees the call to:

llegirany(&any);

in the third line of main, the compiler says "aha, a function named llegirany that returns an int.

Later on, when the compiler gets to the actual function definition for the function, it gets confused -- wait, I thought that llegirany returns an int, and now you tell me it returns a float.

Upvotes: 1

Related Questions