Conflicting types in C

I tried to create a very simple C program 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 I get says:

conflicting types for 'regre'

the previous implicit regre declaration was here

+4
source share
3 answers

This error message tells you what exactly is happening - there is an implicit regre , because you do not define it until main() . Just add the ad ahead:

 double regre(); 

To main() or just move the whole function there.

+7
source
 previous implicit declaration of `regre` was here 

If the function is unknown, the compiler defaults to its int functionname() . In your case, int regre() will be declared here.

 conflicting types for 'regre' 

When your actual double regre() function is noticed, this conflict error occurs. To solve this problem, you must declare a double regre () function before actually using it.

 #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 information about forward declaration, see the following link.

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

+2
source

In C, whenever you use a function by calling by value or calling by reference, these functions will by default be of type int. When you use any function that is its type for this, you need to determine the prototype of the function in the program before calling this function. The best way is to simply define the entire prototype of the function before it becomes the main one, which is a good way of programming. When starting your program, simply define the prototype as double regre (); befor main

 double regre(); main() { 
0
source

Source: https://habr.com/ru/post/1439508/


All Articles