function prototypes

J

junky_fellow

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 main in either of the above mentioned ways
my compiler
does not give any warning. How can it accept two different prototypes
?

Thanx for any help in advance ..
 
V

Vladimir Oka

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 main in either of the above mentioned ways
my compiler
does not give any warning. How can it accept two different prototypes
?

Thanx for any help in advance ..

A C function cannot have two (or more) different prototypes in one
program.

A function with the same name can have different prototypes in all the
different programs you care to use it in, and that includes main() as
well.

For example, these are legal, as they have no relation whatsoever:

---- BEG app1.c ----
int func(void);

int main(void)
{
return func();
}

int func(void)
{
return 0;
}
---- END app1.c ----

---- BEG app2.c ----
double func(double x);

int main(int argc, char *argv[])
{
double y;

y = func(2.0L);

return argc;
}

double func(double x)
{
return -x;
}
---- END app2.c ----

Whereas, this is not:

---- BEG app3.c ----
int func(void);
double func(double x);

int main(void)
{
return func();
}

int func(void)
{
return 0;
}

double func(double x)
{
return -x;
}
---- END app3.c ----

Hope this helps.

Cheers

Vladimir
 
F

Flash Gordon

Can a function have two different prototypes ?

No, apart from main.
> 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 main in either of the above mentioned ways
my compiler
does not give any warning. How can it accept two different prototypes
?

It's magic. Or, to be more precise, main is the one and only exception
because the standard explicitly states that you can use those two forms
for main.
 
S

S.Tobias

Flash Gordon said:
No, apart from main.
This may or may not be correct depending on how you understand
"a function having a prototype" (is it "function _defined_ with
a prototype" - then yes, there can only be one function definition).
You can _declare_ a function multiple times with different prototypes,
or without (provided the declared function types are compatible);
for an example, see n869.txt 6.2.7 ("Compatible type and composite
type") #5.
 
?

=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=

Flash Gordon said:
It's magic. Or, to be more precise, main is the one and only exception
because the standard explicitly states that you can use those two
forms for main.

The Standard also explicitly states that the implementation must not
provide a prototype for main(), so the first part of your answer is
incorrect. If there exists a prototype for main(), other than the one
that results from its definition, it is provided by the application,
and must match the definition.

DES
 
F

Flash Gordon

S.Tobias said:
This may or may not be correct depending on how you understand
"a function having a prototype" (is it "function _defined_ with
a prototype" - then yes, there can only be one function definition).
You can _declare_ a function multiple times with different prototypes,
or without (provided the declared function types are compatible);
for an example, see n869.txt 6.2.7 ("Compatible type and composite
type") #5.

I took different prototypes to mean incompatible prototypes, especially
as the example given was "int main(void)" and "int main(int argc, char
*argv[])" which are clearly incompatible.
 
K

Keith Thompson

Flash Gordon said:
Can a function have two different prototypes ?

No, apart from main.
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 main in either of the above mentioned ways
my compiler
does not give any warning. How can it accept two different prototypes
?

It's magic. Or, to be more precise, main is the one and only exception
because the standard explicitly states that you can use those two
forms for main.

main() is the one and only exception, but not in that way.

Any function other than main() can be defined any way you like. You
can declare a function foo() with any number and type of arguments you
like; likewise for bar() or for just about anything else. main() is
the only function on which the implementation places restrictions: you
can only use one of the two standard forms, or some other
implementation-defined form. main() is unique because it's called by
the environment; other functions are called by your own program.

It would have been more consistent for the implementation to provide a
single prototype for main() and require you to provide a definition
consistent with that prototype. The existing special-case rules are
there for historical reasons.
 
J

Jordan Abel

S.Tobias said:
This may or may not be correct depending on how you understand
"a function having a prototype" (is it "function _defined_ with
a prototype" - then yes, there can only be one function definition).
You can _declare_ a function multiple times with different prototypes,
or without (provided the declared function types are compatible);
for an example, see n869.txt 6.2.7 ("Compatible type and composite
type") #5.

I took different prototypes to mean incompatible prototypes, especially
as the example given was "int main(void)" and "int main(int argc, char
*argv[])" which are clearly incompatible.

well, in that case you can have int foo(double), void foo(int *,
double), double foo(FILE *), or any other imaginable combination. you
certainly can't have _both_ prototypes for main in one program.
 

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
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top