Prototypes

F

fb

Does the C language require you to prototype functions? If it's not
required, is it recommended?
 
J

Jack Klein

Does the C language require you to prototype functions? If it's not
required, is it recommended?

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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
F

fb

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.
Ok...Cool.
So if I want to prototype I would do something like:

#include <stdio.h>

int cube(int c); /* Is this right? */

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?
 
I

Irrwahn Grausewitz

fb 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? */
Right. In a somewhat larger project you would like to put
this in a header file.
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?
Correct.

Irrwahn
 
P

pete

Irrwahn said:
Right. In a somewhat larger project you would like to put
this in a header file.

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.
 
I

Irrwahn Grausewitz

pete 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.
Sometimes it comes in handy to have the name of parameters in the
prototype, so you can see what they mean (though this doesn't apply
to the example we both refer to) when looking at the header file,
with no need to consult the source code (which may not be at hand).

Consider:

void my_error( char*, int, int, int );

This isn't very informative, is it? Whereas:

void my_error( char *message, int level, int number, int do_exit );

talks for itself.
 
L

LibraryUser

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.

I think it would be well to point out to the OP that complete
declaration before use constitutes a proper prototype. It is not
necessary to have a pure prototype declaration in most cases, in
fact it is often even undesirable from a maintenance viewpoint.
The following is a program with complete prototypes:

#include <stdio.h>

void hello(void)
{
printf("Hello");
}

void helloworld(void)
{
hello();
printf(", world");
}

void endofline(void)
{
printf"\n");
}

int main(void)
{
helloworld();
endofline();
return 0;
}

It would be improved if all functions (other than main) were
declared as static, but that complicates the point and is not
germane to the OPs question.
 
K

Kevin Easton

fb said:
Does the C language require you to prototype functions? If it's not
required, is it recommended?

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
 
D

Default User

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?




Brian Rodenborn
 
I

Irrwahn Grausewitz

Why returning int?

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.

But the experts at c.l.c may eventually give a more detailed
answer, perhaps providing some historical background. :)

Irrwahn
 
J

Jack Klein

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.

You seem to have a little confusion here. If you do provide names for
function arguments in a prototype, those names have prototype scope,
which is an official way of saying that 1, the compiler ignores them
and 2, they have no interaction at all with the names of anything
else, including other prototypes or the definition of the same
function.

This is a legal C program:

#include <stdio.h>

void say_hello(char *month, int date);
void say_hello(char *date, int month);

void say_hello(char *frick, int frack)
{
printf("Today is %s %d\n", frick, frack);
}

int main(void)
{
say_hello("September", 2);
return 0;
}

Note three different sets of names in the two prototypes and the
definition of the say_hello() function.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
J

Jack Klein

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.

A prototype also explicitly specifies the return type of the function.
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();

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.
...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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
K

Kevin Easton

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.

This is an explanation of why a function called without a declaration in
scope must be defined as returning int - but it does not address Jack's
assertion (paraphrased) that a function called without a *prototype* in
scope must be defined as returning int - and in fact, it does not. For
example:

#include <stdio.h>

char *foo(); /* Not a prototype */

int main()
{
puts(foo(1));
return 0;
}

char *foo(n)
int n;
{
static const char *a[] = { "Red Fish", "Blue Fish" };
return a[n];
}

- Kevin.
 
D

Default User

Irrwahn said:
This is a legacy from pre ANSI/ISO C; writing sth. like:
return foo(); /* Note the lack of a prototype in scope. */

This is a lack of DECLARATION in scope, not just prototype.
}

foo() /* Note the lack of an explicit return type. */

What does that have to do with prototypes?



Brian Rodenborn
 
D

Dave Thompson

A prototype also explicitly specifies the return type of the function.
Not necessarily in C90: static /* implicit int */ foo (char * x);
In C99 it must, but so must a nonprototype declaration or definition.
In neither case does the presence of the return type *distinguish* a
prototype, and so is not useful to its definition (as a term).
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.
Nits: 'declarator' is a technical term in the syntax and does *not*
include the 'yielded' type specifiers (and qualifiers), nor
storage-class specifiers (or in C99 inline). And 'function scope'
makes no sense. What I think you meant is, more precisely:

In C99 a function definition must include declaration-specifiers
(which was optional in C90) which must be nonempty (no change) and
must contain type-specifier(s) (was optional in C90, defaulting to
int) which together with the declarator (if other than just an
optionally parenthesized identifer) specify the return type.

Similarly in C99 any (syntactic) declaration (including an object
definition, but not a function definition though semantically it also
declares the symbol), must have declaration-specifiers (was optional)
containing a type (was optional).

- David.Thompson1 at worldnet.att.net
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,078
Messages
2,570,572
Members
47,204
Latest member
MalorieSte

Latest Threads

Top