473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Prototypes

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

Nov 13 '05 #1
14 2710
On Tue, 02 Sep 2003 07:26:40 GMT, fb <me@no.net> wrote in comp.lang.c:
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
Nov 13 '05 #2
fb
Jack Klein wrote:
On Tue, 02 Sep 2003 07:26:40 GMT, fb <me@no.net> wrote in comp.lang.c:

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.

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?

Nov 13 '05 #3
fb <me@no.net> wrote in
<HF***********************@news2.calgary.shaw.ca >:
Jack Klein wrote: <SNIP>
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? */

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
--
Air is water with holes in it.
Nov 13 '05 #4
Irrwahn Grausewitz wrote:

fb <me@no.net> wrote in
<HF***********************@news2.calgary.shaw.ca >:
Jack Klein wrote:

<SNIP>
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? */

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.

--
pete
Nov 13 '05 #5
pete <pf*****@mindspring.com> wrote in <3F***********@mindspring.com>:
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.
--
Air is water with holes in it.
Nov 13 '05 #6
Jack Klein wrote:
fb <me@no.net> wrote in comp.lang.c:
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 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.

--
Replies should be to the newsgroup
Chuck Falconer, on vacation.
Nov 13 '05 #7
fb <me@no.net> wrote:
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

Nov 13 '05 #8
Jack Klein wrote:
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
Nov 13 '05 #9
Default User <fi********@company.com> wrote in
<3F***************@company.com>:
Jack Klein wrote:
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?


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
--
Air is water with holes in it.
Nov 13 '05 #10
On Tue, 02 Sep 2003 11:12:55 GMT, pete <pf*****@mindspring.com> wrote
in comp.lang.c:
Irrwahn Grausewitz wrote:

fb <me@no.net> wrote in
<HF***********************@news2.calgary.shaw.ca >:
Jack Klein wrote:

<SNIP>
> 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? */

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.


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
Nov 13 '05 #11
On Tue, 02 Sep 2003 16:37:05 GMT, Kevin Easton
<kevin@-nospam-pcug.org.au> wrote in comp.lang.c:
fb <me@no.net> wrote:
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.


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
Nov 13 '05 #12
Irrwahn Grausewitz <ir*****@freenet.de> wrote:
Default User <fi********@company.com> wrote in
<3F***************@company.com>:
Jack Klein wrote:
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?


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.

Nov 13 '05 #13
Irrwahn Grausewitz wrote:

Default User <fi********@company.com> wrote in
<3F***************@company.com>:
Jack Klein wrote:
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?


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
Nov 13 '05 #14
On Wed, 03 Sep 2003 02:51:53 GMT, Jack Klein <ja*******@spamcop.net>
wrote:
On Tue, 02 Sep 2003 16:37:05 GMT, Kevin Easton
<kevin@-nospam-pcug.org.au> wrote in comp.lang.c:
fb <me@no.net> wrote:
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.


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

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
Nov 13 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

145
by: David MacQuigg | last post by:
Playing with Prothon today, I am fascinated by the idea of eliminating classes in Python. I'm trying to figure out what fundamental benefit there is to having classes. Is all this complexity...
7
by: Michele Simionato | last post by:
So far, I have not installed Prothon, nor I have experience with Io, Self or other prototype-based languages. Still, from the discussion on the mailing list, I have got the strong impression that...
9
by: Grumble | last post by:
Hello everyone, I've come across some strange code. Here it is, stripped down: int main(void) { int *foo; int *bar(); foo = bar(0); return 0;
13
by: DevarajA | last post by:
If a function is visible everywhere, even out the file where it is declared and defined, why should i write prototypes? -- Devaraja (Xdevaraja87^gmail^c0mX) Linux Registerd User #338167...
7
by: junky_fellow | last post by:
Can a function have two different prototypes ? If not , then how can main() have two different prototypes ? int main(void) and int main argc(int argc, char *argv) I mean to say, if I declare...
1
by: petermichaux | last post by:
Hi, I have searched the archives but didn't find the questions and answers I am looking for. I have been looking at Prototype.js quite a bit lately as I need to create a very small library of...
20
by: Ari Krupnik | last post by:
scripts can add methods to the prototypes of builtin objects in JaavScript. I can assign functions to String.prototype.*, for instance. I want to add a method to Node, but when I try to execute...
73
by: Steph Barklay | last post by:
Hi, I'm currently taking a data structures course in C, and my teacher said that function prototypes are not allowed in any of our code. He also said that no professional programmers use function...
4
by: robtyketto | last post by:
Greetings, I’m writing a research report on justifying disposable prototypes. The problem being there is a wealth of material regarding failures in the IT press and some of these put partial...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.