www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - return type of main (should be int or void)

Some test programs (one-liners):

 void main() {}
$ dmd main1.d
 void main(char[][] args) {}
$ dmd main2.d
 void main(char[] string) {}
$ dmd main3.d main3.d:1: function main3.main parameters must be main() or main(char[][] args) So far, so good. But then comes:
 int main() { return 0; }
$ dmd main4.d
 int main(char[][] args) { return 0; }
$ dmd main5.d
 char[] main() { return "EXIT_SUCCESS"; }
$ dmd main6.d i.e. No errors (on Mac OS X), until you run it:
 $ ./main6; echo $?
 Bus error
similar on Linux, when using the GDC compiler:
 $ ./main6; echo $?
 Segmentation fault
but, if you compile it on Linux X86: (with DMD)
 $ ./main6; echo $?
 12
I think that main should be *forced* to return int or void, where "void" would mean that the program would return a 0... This would shorten small programs, from:
 int main()
 {
     printf("Hello, World!\n");
     return 0;
 }
to:
 import std.stdio;
 void main()
 {
     writefln("Hello, World!");
 }
At least some constants from C's /usr/include/stdlib.h could be used, to make D programs more self-documenting:
 import std.stdio;
 import std.c.stdlib;
 int main()
 {
     writefln("Hello, World!");
     return EXIT_SUCCESS;
 }
In summary, "void main()" should be either defined or invalid - to avoid this: http://users.aber.ac.uk/auj/voidmain.shtml ? Currently it has different results for DMD and GDC compilers.... --anders PS. "12" was of course (?) the length of the string "EXIT_SUCCESS" See my previous post, currently DMD returns register EAX (!): http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/13127
Jan 12 2005