whats prototype all about? - Minix
This is a discussion on whats prototype all about? - Minix ; hi
ive just began reading the minix book and i already have a question:
what's the _prototype thing all about? (page 132)? I've never seen this
syntax before; normally, when i declare a function prototype in c i
would simply ...
-
whats prototype all about?
hi
ive just began reading the minix book and i already have a question:
what's the _prototype thing all about? (page 132)? I've never seen this
syntax before; normally, when i declare a function prototype in c i
would simply write
returntype functioname(list_of_parameters);
w/o having to put anything like _prototype before it..
why would anyone decide to make things more complicated than they are?
thanx
martin
-
Re: whats prototype all about?
sancho1980 wrote:
> hi
> ive just began reading the minix book and i already have a question:
> what's the _prototype thing all about? (page 132)? I've never seen this
> syntax before; normally, when i declare a function prototype in c i
> would simply write
Well, I am not sure how familiar you are with C programming, but if you
don't define a function in a header, you have to "prototype" it in the
program. This basically means for some function, say:
int hi_world() {
printf("Goodbye, Cruel World!\n");
return 0;
}
You first have to prototype it by writing BEFORE it is defined:
int hi_world();
in the same program. The prototype macro basically does the same thing,
as far as I know, try writing a program (a small program, like a hello
world program) that uses the Macro definition and compile it on C. See
what happens when you prototype a function with the macro.
-
Re: whats prototype all about?
> The prototype macro basically does the same thing,
Right, that's my point...why would anyone care about using the
prototype macro (which involves MORE typing than prototyping the
function the normal way)?
-
Re: whats prototype all about?
Hi,
"sancho1980" writes:
> > The prototype macro basically does the same thing,
>
> Right, that's my point...why would anyone care about using the
> prototype macro (which involves MORE typing than prototyping the
> function the normal way)?
>
The _PROTOTYPE can be set to expand differently dependend on what
compiler you have.
Look at
_PROTOTYPE( int debug, (const char *buffer, int instruction) );
Pre-ANSI K&R compilers don't know prototypes, so
#define _PROTOTYPE(f,pars) f()
For ANSI compilers instead
#define _PROTOTYPE(f,pars) f pars;
Basically this is/was a compatibilty measure for the time of
transition from K&R to ANSI C. AFAIK since Minix 1.7 ACK is an ANSI
compiler (knows prototypes). It must be quite some time since anyone
has tried to compile Minix under K&R C, so _PROTOTYPE must perhaps be
seen as an historical artefact.
On the other side. I'm still waiting for PDP/11-Minix to run at
Supnick's simh simulator. Is there an ANSI (cross-) compiler for
PDP/11? If not, _PROTOTYPE would come handy.
BTW: This is a rather widely used trick. You'll find similar macros
(called _P or __P) in almost all "portable" library header files, like
i.e. the gnu libc.
Regards -- Markus
-
Re: whats prototype all about?
In article <1160779087.850559.43830@e3g2000cwe.googlegroups.co m>,
sancho1980 wrote:
>> The prototype macro basically does the same thing,
>
>Right, that's my point...why would anyone care about using the
>prototype macro (which involves MORE typing than prototyping the
>function the normal way)?
It is part of the transition from K&R C to ANSI standard C.
--
That was it. Done. The faulty Monk was turned out into the desert where it
could believe what it liked, including the idea that it had been hard done
by. It was allowed to keep its horse, since horses were so cheap to make.
-- Douglas Adams in Dirk Gently's Holistic Detective Agency
-
Re: whats prototype all about?
sancho1980 wrote:
>> The prototype macro basically does the same thing,
>
> Right, that's my point...why would anyone care about using the
> prototype macro (which involves MORE typing than prototyping the
> function the normal way)?
>
I think this is a leftover from the old times of pre ANSi C compilers
that did not make argument checking.
If you are confident that your code will never be compiled with an old
compiler you can avoid using the _PROTOTYPE macro.
Ciao
Giovanni
--
A computer is like an air conditioner,
it stops working when you open Windows.
Registered Linux user #337974 < http://giovanni.homelinux.net/ >
-
Re: whats prototype all about?
Pablo Rodriguez wrote:
> sancho1980 wrote:
> > hi
> > ive just began reading the minix book and i already have a question:
> > what's the _prototype thing all about? (page 132)? I've never seen this
> > syntax before; normally, when i declare a function prototype in c i
> > would simply write
>
> Well, I am not sure how familiar you are with C programming, but if you
> don't define a function in a header, you have to "prototype" it in the
> program. This basically means for some function, say:
>
> int hi_world() {
> printf("Goodbye, Cruel World!\n");
> return 0;
> }
> You first have to prototype it by writing BEFORE it is defined:
>
> int hi_world();
>
> in the same program. The prototype macro basically does the same thing,
> as far as I know, try writing a program (a small program, like a hello
> world program) that uses the Macro definition and compile it on C. See
> what happens when you prototype a function with the macro.
excuse me, but protyping is NOT necessary if the datadefs and functions
are
defined in order before use; that doesnt mean a prototype or header is
necessary,
unless you're #including alot of stuff like stdio.h
ie, main() should BE the last function defined at the end of the file.
-
Re: whats prototype all about?
ok, that explains it, thanx
M E Leypold wrote:
> Hi,
>
> "sancho1980" writes:
>
> > > The prototype macro basically does the same thing,
> >
> > Right, that's my point...why would anyone care about using the
> > prototype macro (which involves MORE typing than prototyping the
> > function the normal way)?
> >
>
>
> The _PROTOTYPE can be set to expand differently dependend on what
> compiler you have.
>
> Look at
>
> _PROTOTYPE( int debug, (const char *buffer, int instruction) );
>
>
> Pre-ANSI K&R compilers don't know prototypes, so
>
> #define _PROTOTYPE(f,pars) f()
>
> For ANSI compilers instead
>
> #define _PROTOTYPE(f,pars) f pars;
>
> Basically this is/was a compatibilty measure for the time of
> transition from K&R to ANSI C. AFAIK since Minix 1.7 ACK is an ANSI
> compiler (knows prototypes). It must be quite some time since anyone
> has tried to compile Minix under K&R C, so _PROTOTYPE must perhaps be
> seen as an historical artefact.
>
> On the other side. I'm still waiting for PDP/11-Minix to run at
> Supnick's simh simulator. Is there an ANSI (cross-) compiler for
> PDP/11? If not, _PROTOTYPE would come handy.
>
> BTW: This is a rather widely used trick. You'll find similar macros
> (called _P or __P) in almost all "portable" library header files, like
> i.e. the gnu libc.
>
> Regards -- Markus
-
Re: whats prototype all about?
sancho1980 wrote:
> hi
> ive just began reading the minix book and i already have a question:
> what's the _prototype thing all about? (page 132)? I've never seen this
> syntax before; normally, when i declare a function prototype in c i
> would simply write
>
> returntype functioname(list_of_parameters);
>
> w/o having to put anything like _prototype before it..
> why would anyone decide to make things more complicated than they are?
>
> thanx
>
> martin
>
It's a marco that allows for creation of prototypes that are ANSI for
ANSI C compilers, and K&R for olf K&R compilers.
A type defintition in ANSI C is:
int memset(void *buf, int bit, int len);
K&R looks like this:
int memset(char *, int, int);
(Note that `void *' isn't valid K&R C, and `char *' must be used for
pointers instead)
The beginning of a function also looks different from K&R and ANSI:
ANSI:
int memset(void *buf, int bit, int len)
{
...
K&R:
int memset(buf, bit, len)
char *buf;
int bit;
int len;
{
...
I hope that clears it up for you.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFFMreVsjeOFtd+nycRAsYfAJ9x2W7gYNMUIL8BIix6dW eTKiKq+wCbB9R8
nioskNspCIIhvVDKETi1i44=
=7HCW
-----END PGP SIGNATURE-----