F
fb
Does the C language require you to prototype functions? If it's not
required, is it recommended?
required, is it recommended?
Does the C language require you to prototype functions? If it's not
required, is it recommended?
Ok...Cool.Jack said:IMNSHO, the single greatest improvement ever made to the C language
was the addition of prototypes. The word "recommended" does not even
come close to describing the impact of properly used prototypes in
greatly reducing coding errors. Any professional organization worthy
of the name will certainly require programming employees to produce
and use adequate prototypes.
As for required by the language, there are a few cases where it is
not.
If a function returns an int, does not have a variable argument list,
and all its arguments have a type supported by the default argument
promotions, then it is not a language requirement to have a prototype
in scope.
Under the latest version of the C standard, not too widely implemented
in compilers as of yet, it is required to have at least a declaration
of even those functions that are not required to have prototypes.
Right. In a somewhat larger project you would like to putfb said:Jack Klein wrote:
Ok...Cool.
So if I want to prototype I would do something like:
#include <stdio.h>
int cube(int c); /* Is this right? */
Correct.int main(void)
{
int x = 10, y = 7;
int v;
printf("X = %d\n",x);
v = cube(x);
printf("Now X = %d\n\n",v);
printf("Y = %d\n",y);
v = cube(y);
printf("Now Y = %d\n\n",v);
return 0;
}
int cube(int c)
{
return (c*c*c);
}
Correct?
Irrwahn said:Right. In a somewhat larger project you would like to put
this in a header file.
Sometimes it comes in handy to have the name of parameters in thepete said:You don't need the name of the parameter, in this case "c"
in a prototype.
int cube(int);
Sometimes that comes in handy,
when you change the name of a parameter
and the prototype is in a header file.
Jack said:IMNSHO, the single greatest improvement ever made to the C
language was the addition of prototypes. The word "recommended"
does not even come close to describing the impact of properly
used prototypes in greatly reducing coding errors. Any
professional organization worthy of the name will certainly
require programming employees to produce and use adequate
prototypes.
As for required by the language, there are a few cases where
it is not.
If a function returns an int, does not have a variable argument
list, and all its arguments have a type supported by the
default argument promotions, then it is not a languageprototype
requirement to have a in scope.
Under the latest version of the C standard, not too widely
implemented in compilers as of yet, it is required to have at
least a declaration of even those functions that are not
required to have prototypes.
fb said:Does the C language require you to prototype functions? If it's not
required, is it recommended?
Jack said:If a function returns an int, does not have a variable argument list,
and all its arguments have a type supported by the default argument
promotions, then it is not a language requirement to have a prototype
in scope.
Why returning int?
You don't need the name of the parameter, in this case "c"
in a prototype.
int cube(int);
Sometimes that comes in handy,
when you change the name of a parameter
and the prototype is in a header file.
There is a large amount of confusion about what a function prototype
actually is. Many people seem to use it to refer to a forward
definition (declaring the function before use), but a prototype is
actually a declaration or definition that specifies the number and types
of the function arguments.
The former (declaring a function before use) is not required by
the language in all cases - if it is left out it is as if you declared
it thus:
int func();
...which is a non-prototype declaration. If the function definition
defines the function as returning an 'int', and all calls to the
function give the correct number and type of parameters after default
argument promotions, and the function is not variadic, then it is OK
(but considered extremely bad style). If those conditions are not met,
then it is an error.
A prototype declaration is also not a strict requirement of the
language, though non-prototype declarations are obsolescent and should
be avoided. They have the same conditions as for undeclared functions
above, with the exception that the return type is specified by the
declaration.
An example of a prototyped declaration is:
void func(int, int);
whereas a non-prototyped declaration has empty parentheses following the
function name.
- Kevin
Irrwahn Grausewitz said:This is a legacy from pre ANSI/ISO C; writing sth. like:
int main( void )
{
return foo(); /* Note the lack of a prototype in scope. */
}
foo() /* Note the lack of an explicit return type. */
{
/* .... */
return 0;
}
the compiler assumes that foo will return an int and the program
will work (though the compiler should throw out a diagnostic).
Think of this as default behaviour when no explicit return type
was given.
Irrwahn said:This is a legacy from pre ANSI/ISO C; writing sth. like:
return foo(); /* Note the lack of a prototype in scope. */
}
foo() /* Note the lack of an explicit return type. */
Not necessarily in C90: static /* implicit int */ foo (char * x);A prototype also explicitly specifies the return type of the function.
Nits: 'declarator' is a technical term in the syntax and does *not*That is no longer true under the current C standard (i.e., October
1999) although there are few compilers claiming a significant degree
of conformance with it.
Implicit int is dead in C, as of four years ago. There must be at
least a declarator with return type for a function scope, or a
compiler conforming to the current standard must issue a diagnostic.
And all declarators, including those for functions, must include a
type.
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.