Parameter names/types of main function

567 views
Skip to first unread message

Johannes Schaub (litb)

unread,
Nov 12, 2009, 5:22:36 PM11/12/09
to
Is any of the following guaranteed to compile?

int main(int argc, char **argv) { }
int main(int, char **) { }
int main(int argc1, char* argv1[]) { }
int main(int const argc, char* argv[]) { }
int main(int argc, char *argv[]) try { } catch(...) { }

The Standard seems to mandate the following definition if we use parameters
(3.6.1)

int main(int argc, char *argv[]) { /* ... */ }

To what extent should anything of the above form be taken literally? Can any
parameter be const, or could an implementation literally insert some code
that relies on parameter names?

--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std...@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Kaz Kylheku

unread,
Nov 12, 2009, 8:46:25 PM11/12/09
to
On 2009-11-12, Johannes Schaub (litb) <schaub-...@web.de> wrote:
> Is any of the following guaranteed to compile?

Collateralized by what? Your money back? Additional damages? ;)

> The Standard seems to mandate the following definition if we use parameters
> (3.6.1)
>
> int main(int argc, char *argv[]) { /* ... */ }

Yes, in fact that is what it literally says. This is a surprising defect.

They just had to cut and paste some effective text from the C standard:

``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;9) or in some other implementation-defined manner.
...
----
9 Thus, int can be replaced by a typedef name defined as int, or the type of
argv can be written as char ** argv, and so on.'' [C99, 5.1.2.2.1]

> To what extent should anything of the above form be taken literally?

To the full extent. The standard says what it says. Some C++ implementor could
interpret it literally. That's too bad for your program, where you called
these arguments something else. Your best hope is that enough programs violate
this rule that implementors have to support alternate names and forms.
But in fact if you call it int main(int foo, char **bar), and everything
works fine, you are using a form that the letter of the standard does not
require implementors to support.

Joe Smith

unread,
Nov 15, 2009, 1:51:19 AM11/15/09
to

"Johannes Schaub (litb)" <schaub-...@web.de> wrote in message
news:hdhq21$3me$02$1...@news.t-online.com...

>
> Is any of the following guaranteed to compile?
>
> int main(int argc, char **argv) { }
> int main(int, char **) { }
> int main(int argc1, char* argv1[]) { }
> int main(int const argc, char* argv[]) { }
> int main(int argc, char *argv[]) try { } catch(...) { }
>
> The Standard seems to mandate the following definition if we use parameters
> (3.6.1)
>
> int main(int argc, char *argv[]) { /* ... */ }
>
> To what extent should anything of the above form be taken literally? Can any
> parameter be const, or could an implementation literally insert some code
> that relies on parameter names?
>

Kaz already having answered this question, I would like to propose my
prefered solution.

Instead of the standard specifying the format of defintions for main
that must be accepted, it could specify that implementations must
support any definition of main that matches one of these declarations:

int main();
int main(int argv, char *argv[]);

By doing it like that, all equivlent formulations, using typedefs or
char**, etc would be required to be accepted. In particular, all of
Johannes' proposed definition forms, except for the one with const
argv would be acceptable. This form of specification allows the
standard to dicuss the roles of argv and argc without mandating that
hey be named such in the definition, since names need not match. It
also allows the parameters to be unnamed in the definition, which even
the C99 wording does not allow (since it lacks the concept of unnamed
parameters).

Perhaps most importantly, it would allow defintions of main in the
following format to be portable to all conformant impmementations,
which is of value for compatibility with common practice in C:

int main(void) { /*...*/ }


Further, the only format I can think of that this would allow that
might not be well supported in all implementations is the form using
the function-try-block, which could be forbidden for main with a
single additional sentence.

Reply all
Reply to author
Forward
0 new messages