2 ms·
There's one definition of `bar` there: It's a function that accepts an `int` as a parameter, and outputs a `char`.
by the__alchemist 5y ago
There's one definition of `bar` there: It's a function that accepts an `int` as a parameter, and outputs a `char`.
- Jtsummers 5y agoDid I say there were two definitions? I said there are two declarations. Try using a function with a return type other than int before it has been defined or declared in C and see what happens when you compile it.
- the__alchemist 5y agoThe function is declared/defined/described/written once, and used once. There is no ambiguity. I don't understand your point about using a function that hasn't been declared. Of course it won't work! I understand your point about using header files as interfaces to third-party code, but there are ways of doing that that don't involve duplicating all functions, structs etc.
- Jtsummers 5y agoThe function is used once, and because it occurs before the actual definition (and there was no forward declaration) an implicit declaration occurs (the erroneous int bar()). When the function definition is later reached by the compiler, its signature doesn't match the implicit declaration's signature, causing the problem. If the function signature matches the implicit declaration, then there is no problem: int main(int argc, char* argv[]) { printf("%d\n", foo(10)); return 0; } int foo(int n) { return n - 1; } Will work just fine, giving you only a warning but will print out "9" as expected.
- the__alchemist 5y agoYikes! The implicit declaration, if used before (as in a lower line number than the function definition?) sounds like a trap, and at the root of this.
- ylyn 5y agoYes. But that's C. You can't fix it without fundamentally altering the language.
- lukas099 5y ago> an implicit declaration occurs (the erroneous int bar() I still don't understand; why does the implicit declaration assume int bar() is the signature when bar is being passed an int and returning a value to a char?
- Jtsummers 5y agoBecause C doesn't do type inference.
- Liquid_Fire 5y agoYou don't need type inference to handle this correctly, you just need to wait until you've parsed the rest of the file and seen the signatures of all functions.