1

Conceptual question. Consider the following code:

#include<stdio.h>

int brasa(int, float); 
int brasa(int, int); 
float brasa(int, int);

int main(){

return 0;
}

The compiler gives the following errors:

Line 4: error: conflicting types for 'brasa'
Line 3: note: previous declaration of 'brasa' was here
Line 5: error: conflicting types for 'brasa'
Line 3: note: previous declaration of 'brasa' was here

Which kind of message is that? Another conceptual question: do the three declarations all declare the same function?

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
user3482381
  • 83
  • 1
  • 7
  • 1
    "What kind of message is that?" It's an error message, telling you there is an error in the code. "Do the three declarations all declare the same function?" Conceptually, no, none of them declare anything, because the code fails to compile. You could arguably state that the first one declares something and the others are errors, but if the code doesn't compile, it really doesn't matter much... – twalberg Apr 07 '14 at 17:37
  • Why there's a violation of the type-compatibility conventions? – user3482381 Apr 07 '14 at 18:01
  • 2
    "type-compatibility conventions" don't come into play in declarations of functions. As @haccks mentions in his answer, C allows only a single declaration of a function with a particular name, with no overloading on different argument/return types. `float f = 1;` is a different story - that's where type-compatibility matters, and the compiler will automatically insert appropriate conversions as needed... – twalberg Apr 07 '14 at 18:13

1 Answers1

3

In C you can't declare a variable/function multiple times with different types (incompatible types). There is no function overloading, unlike in C++, in C.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • @KeithThompson; Hey, what's the reason here ? I tried with global variables and it is working too! – haccks Apr 07 '14 at 21:42
  • 2
    You can have multiple *declarations* as long as they're compatible. The problem in the OP's code is that the declarations aren't compatible. (I'm not sure that's the exact technical term, but the rules are specifed in the standard.) You can't have multiple *definitions*. – Keith Thompson Apr 07 '14 at 21:47
  • But this doesn't hold true in case of local variables. – haccks Apr 07 '14 at 21:51
  • 2
    @KeithThompson; I got the answer. I changed my words. Thanks for the nitpicking :) – haccks Apr 07 '14 at 22:02