By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,685 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

function prototypes

P: n/a
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 ..

Jan 18 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
ju**********@yahoo.co.in wrote:
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

Jan 18 '06 #2

P: n/a
ju**********@yahoo.co.in wrote:
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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 18 '06 #3

P: n/a
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
ju**********@yahoo.co.in wrote:
Can a function have two different prototypes ?


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.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Jan 18 '06 #4

P: n/a
Flash Gordon <sp**@flash-gordon.me.uk> writes:
ju**********@yahoo.co.in wrote:
Can a function have two different prototypes ?

No, apart from main.
If not, then how can main() have 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.


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
--
Dag-Erling Smørgrav - de*@des.no
Jan 18 '06 #5

P: n/a
Flash Gordon <sp**@flash-gordon.me.uk> writes:
ju**********@yahoo.co.in wrote:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 18 '06 #6

P: n/a
S.Tobias wrote:
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
ju**********@yahoo.co.in wrote:
Can a function have two different prototypes ?

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.


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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 18 '06 #7

P: n/a
On 2006-01-18, Flash Gordon <sp**@flash-gordon.me.uk> wrote:
S.Tobias wrote:
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
ju**********@yahoo.co.in wrote:
Can a function have two different prototypes ?
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.


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.
Jan 19 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.