468,539 Members | 1,543 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,539 developers. It's quick & easy.

int main(void) Why void?

Hello everyone,

I have noticed everyone using "int main(void)".But doesnt the
standard pronounces it as
"int main(int argc,char *argv[])".And if i don't specify arguments,why
can't i simply put it as "int main()"

Rahul

Apr 6 '06 #1
6 2187
ra*********@gmail.com schrieb:
I have noticed everyone using "int main(void)".But doesnt the
standard pronounces it as
"int main(int argc,char *argv[])".And if i don't specify arguments,why
can't i simply put it as "int main()"


See FAQ 11.12a.

As it is usually better to make every function declaration a
prototype, it is only consistent and sensible to do the same
for main(). Consider it better style.
As for "char *argv[]": I'd rather use the equivalent but more
straightforward "char **argv".

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 6 '06 #2
ra*********@gmail.com wrote:

Hello everyone,

I have noticed everyone using "int main(void)".But doesnt the
standard pronounces it as
"int main(int argc,char *argv[])".
It pronounces it in both of those two ways.

N869
5.1.2.2.1 Program startup
[#1] The function called at program startup is named main.
The implementation declares no prototype for this function.
It shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv,
though any names may be used, as they are local to the
function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent; or in some other implementation-defined
manner.
And if i don't specify arguments,why
can't i simply put it as "int main()"


You can, but non prototype function declarations, like
int main(),
are officially an obsolecent feature of the language.

N869
6.11.4 Function declarators
[#1] The use of function declarators with empty parentheses
(not prototype-format parameter type declarators) is an
obsolescent feature.

--
pete
Apr 6 '06 #3
ra*********@gmail.com wrote:
....And if i don't specify arguments,why
can't i simply put it as "int main()"


The two forms are not equivalent.
"int function_name(void);" declares a function with no parameters.
"int function_name();" declares a function with an unknown number of
parameters.
Apr 6 '06 #4
ra*********@gmail.com writes:
I have noticed everyone using "int main(void)".But doesnt the
standard pronounces it as
"int main(int argc,char *argv[])".And if i don't specify arguments,why
can't i simply put it as "int main()"


In C, "int main()" declares main as a function taking an unspecified
number and type of arguments. This goes back to the old-style
function declarations that existed before prototypes were introduced;
old-style declarations are still legal.

<OT>
In C++, "int main()" means specifically that main takes no arguments.
This is simpler and more consistent, and it's possible because C++
doesn't support old-style function declarations. C's use of the "void"
keyword here is necessary for backward compatibility.
</OT>

It's been said that the main function is unique because it can be
defined in more than one way. In fact, it's unique because the
language *restricts* the ways it can be defined. If you want to have
a function "foo", you can define it any way you like. The "main"
function is different because, unlike "foo", it's called by the
external environment; the language imposes restrictions on how you can
define it so that the environment can figure out how to call it
properly. It would have been simpler to restrict it even further,
allowing only one form for main, but it was decided that the extra
flexibility (for programs that don't use command line arguments) was
worth the slight extra burden on the implementation.

--
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.
Apr 6 '06 #5
Me
pete wrote:
ra*********@gmail.com wrote:

And if i don't specify arguments,why
can't i simply put it as "int main()"


You can, but non prototype function declarations, like
int main(),
are officially an obsolecent feature of the language.

N869
6.11.4 Function declarators
[#1] The use of function declarators with empty parentheses
(not prototype-format parameter type declarators) is an
obsolescent feature.


It's 6.11.6 in the real standard but this just has to do with phasing
out K&R stuff. The relevant part that allows main to be defined with an
empty parameter list is:

6.7.5.3/14 "... An empty list in a function declarator that is part of
a definition of that function specifies that the function has no
parameters. ..."

Which is highly unlikely to go away.

Apr 7 '06 #6
Me wrote:

pete wrote:
ra*********@gmail.com wrote:

And if i don't specify arguments,why
can't i simply put it as "int main()"


You can, but non prototype function declarations, like
int main(),
are officially an obsolecent feature of the language.

N869
6.11.4 Function declarators
[#1] The use of function declarators with empty parentheses
(not prototype-format parameter type declarators) is an
obsolescent feature.


It's 6.11.6 in the real standard but this just has to do with phasing
out K&R stuff.
The relevant part that allows main to be defined with an
empty parameter list is:

6.7.5.3/14 "... An empty list in a function declarator that is part of
a definition of that function specifies that the function has no
parameters. ..."

Which is highly unlikely to go away.


Doesn't seem any less likely to go away than ...
6.7.5.3/14
"The empty list in a function declarator that is not part of a
definition of that function specifies that no information about the
number or types of the parameters is supplied."

One's just as obsolescent as the other.

--
pete
Apr 7 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.