473,508 Members | 2,216 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function prototypes

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
7 2356
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
3807
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
6
7958
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the...
6
4980
by: Robbie Hatley | last post by:
I'm maintaining a software project with 134 C++ files, some of them huge (as much as 10,000 lines each), and very few prototypes. The author's attitude towards prototypes was like this: ...
9
1644
by: wizwx | last post by:
what does the following mean? int func2(); According to C++, it surely means func2 is a function that takes no argument and returns an integer. But what about in C? Does it have the same...
1
5544
by: Noah Roberts | last post by:
Trying to use boost::function in a C++/CLI program. Here is code: pragma once #include <boost/function.hpp> #include <boost/shared_ptr.hpp> #include <vector> using namespace System;
16
7570
by: arne | last post by:
Hi all, imagine I call a function, but omit one of the parameters, like: foo.c: void foo( int a, int b ) { /* do something with a and b */ return; }
73
3371
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...
29
8050
by: Ravishankar S | last post by:
Dear C Experts, While prepating a content for a C course,I made section on function prototypes. Could you kindly provide me your comments on its correctness. Thank you ! Q12: What is the...
16
3255
by: Xiaoxiao | last post by:
Hi, I got a C library, is there a way to view the public function names in this library so that I can use in my C program? Thanks.
0
7331
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
7391
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...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1564
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.