main declaration

bearophile bearophileHUGS at
Fri Mar 19 03:23:36 PDT 2010

Ludovic A. Wrote:

> According to the documentation,main() must be declared using one of the
> following forms:
> void main() { ... }
> void main(char[][] args) { ... }
> int main() { ... }
> int main(char[][] args) { ... }

If you use "int main" then you have to add a return, possibly returning std.c.stdlib.EXIT_SUCCESS or std.c.stdlib.EXIT_FAILURE. If you use void it uses a EXIT_SUCCESS.

> However with the latest D2 compiler I can write:
> import std.stdio;
> void main (string[] args) {
>   writeln("main with string[]");
> }

In D1 Tango doesn't define the alias string that's char[]. But the (string[] args) can be used in D1 too, with Phobos:

In D2 string means something different, it's immutable(char)[] that is a mutable array of immutable chars. I hope Tango2 will define string the same way.
So in D1 (string[] args) is the same as (char[][] args), but in D2 they are a little different things. Even if D2 accepts both forms, I think that modifying the contents of args is not a good practice, so in D2 I suggest to use (string[] args), that has immutable chars.
In practice in D2 you can even use main(immutable string[] args), that I think is the best form :-)


More information about the Digitalmars-d-learn mailing list