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() {}

 void main(char[][] args) {}

 void main(char[] string) {}

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; }

 int main(char[][] args) { return 0; }

 char[] main() { return "EXIT_SUCCESS"; }

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 (!): digitalmars.D/13127
Jan 12 2005