D
DevarajA
If a function is visible everywhere, even out the file where it is
declared and defined, why should i write prototypes?
declared and defined, why should i write prototypes?
If a function is visible everywhere, even out the file where it is
declared and defined, why should i write prototypes?
DevarajA ha scritto:
I've just read that they're used for type checking when calling external
functions. Is that true?
The thing described by the OP (who,oddly enough, has the same nym as you) is
not an external function. He said it was *visible*. C does compiling in a
forward direction and it must know the argument types when it compiles. So
if you do the physical definition prior to the first usage you don't need a
prototype. This is sometimes called "Pascal style". The prototypes for
external functions are usually in a visible header file.
DevarajA said:so why this works and only gives "implicit declaration" warnings?
int main()
{
a(8,9);
b(9,9.5);
c(9.5,10);
return 0;
}
int a(int x, int y){return 0;}
int b(int x, double y){return 0;}
int c(double x, int y){return 0;}
:
There was a time when something called K&R C was a more or less de facto
standard. Before there was a real standard. It assumed that everything was
int. Your compiler is in a mode such that it accepts the old dialect.
DevarajA said:If a function is visible everywhere, even out the file where it is
declared and defined, why should i write prototypes?
osmium said:The thing described by the OP (who,oddly enough, has the same nym as you) is
not an external function. He said it was *visible*. C does compiling in a
forward direction and it must know the argument types when it compiles. So
if you do the physical definition prior to the first usage you don't need a
prototype. This is sometimes called "Pascal style". The prototypes for
external functions are usually in a visible header file.
John said:If a function is visible everywhere, even out the file where it is
declared and defined, why should i write prototypes?
You need to have a prototype declaration in scope if the function is
being called before it has been defined. [...]
Eric said:John said:If a function is visible everywhere, even out the file where it is
declared and defined, why should i write prototypes?
You need to have a prototype declaration in scope if the function is
being called before it has been defined. [...]
No: You need a declaration, but the declaration is not
required to provide a prototype. (Under C90 rules, even
the declaration is sometimes optional.)
Prototypes are a Good Thing; I cannot think of a case
where a prototype-less declaration wouldn't be improved by
adding a prototype. However, the only time a prototype is
actually required is when the function has a variable-length
argument list. All fixed-length prototypes can be omitted,
if you've got masochistic tendencies and are willing to suffer
opprobrium from others who work with your awful code.
DevarajA said:osmium ha scritto:
So for that old standard a call to a undeclared function f1 is an
implicit declaration (as int f1(...))?
If a function is visible everywhere, even out the file where it is declared
and defined, why should i write prototypes?
If a function is visible everywhere, even out the file where it is
declared and defined, why should i write prototypes?
Nit: Implicit int, and implicit function declaration, which isIt was not removed from the C89 standard, only deprecated.
No, definitely not. It assumes are return type of in and a FIXED number
and types of arguments (after they have undergone the default argument
promotions) based on the first call it sees. Although this works if you
get it exactly right, it does not work if:
1) The function returns something other than int
2) You don't get the list of arguments right
3) the functions has varargs (, ...) parameters
4) The function takes parameters of types that will be promoted (e.g. char)
The compiler does not have to diagnose (generate error or warning) for
ANY of these problems. So you should always make sure you have the
correct prototype in scope.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.