5

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

dda
  • 6,030
  • 2
  • 25
  • 34
karlelizabeth
  • 157
  • 2
  • 3
  • 13
  • if i replace in the regre function doble for int type the program runs... but i need to return a double from a function. – karlelizabeth Oct 13 '12 at 03:41
  • 4
    You should declare the function before you use it. – Toribio Oct 13 '12 at 03:41
  • Note: Maybe unlike to you are thinking, `double regre()` make a function with undefined number of arguments of unknow types. Check out http://stackoverflow.com/questions/693788/c-void-arguments – Jack Oct 13 '12 at 04:10

3 Answers3

9

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.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 1
    thanks a lot! i didnt know that! this is my first program in c using functions... thanks!!! – karlelizabeth Oct 13 '12 at 03:47
  • @elizabeth - if you're a beginner, and it sure sounds like you are, you might want to check out [clang/LLVM](http://clang.llvm.org). The error messages from clang are a lot better than most compilers, and that will help you get started. – Carl Norum Oct 13 '12 at 03:48
2
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

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
0

In C whenever your using a function by either call by value or call by reference , those functions by default they will be int type . When your using any function which is the type of it for that you need to define the function prototype in the program before calling that function . The best way is just define all the function prototype before the main it self that is the good way of programming . Coming to your program just define the prototype as double regre(); befor main

double regre();
    main()
      {