int main(void) Why void?

R

rahulsinner

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
 
M

Michael Mair

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
 
P

pete

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.
 
R

Roberto Waltman

....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.
 
K

Keith Thompson

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.
 
M

Me

pete said:
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.
 
P

pete

Me said:
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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top