www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - return code of main

reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Would it be possible to change the D declaration
"void main() {}" to return 0 back to the shell ?

Currently it seems to return "1", or failure*...
(as per C macros: EXIT_SUCCESS=0, EXIT_FAILURE=1)


Which makes that you must use the somewhat longer
"int main() { return 0; }", just to make it happy.

(if you actually wanted to read arguments and return
a code, you'd probably use: "int main(char[] args)")


Allowing "void main()" would be a nice improvement
over ANSI C, which has the same main restrictions.

That is: if you declare main as void, the compiler
should explicitly insert a "return 0;" equivalent.

--anders

* For some reason, DMD on Linux returns a "12" ???

   Seems like all bets are off now, you can even declare
   stuff like "float main()" (for fuzzy return states?)

   GCC does at least whine, in the same situation:
   "warning: return type of `main' is not `int'"
Nov 29 2004
parent reply Thomas <Thomas_member pathlink.com> writes:
In article <cof1cb$p8$1 digitaldaemon.com>, Anders says;
Would it be possible to change the D declaration
"void main() {}" to return 0 back to the shell ?

Currently it seems to return "1", or failure*...
(as per C macros: EXIT_SUCCESS=0, EXIT_FAILURE=1)


Which makes that you must use the somewhat longer
"int main() { return 0; }", just to make it happy.

(if you actually wanted to read arguments and return
a code, you'd probably use: "int main(char[] args)")


Allowing "void main()" would be a nice improvement
over ANSI C, which has the same main restrictions.

That is: if you declare main as void, the compiler
should explicitly insert a "return 0;" equivalent.

--anders

* For some reason, DMD on Linux returns a "12" ???

   Seems like all bets are off now, you can even declare
   stuff like "float main()" (for fuzzy return states?)

   GCC does at least whine, in the same situation:
   "warning: return type of `main' is not `int'"
If you play a little around, you'll notice that the "return" value is pseudo random. I guess that whatever value is stored in EAX will be "returned" to the shell. Thomas
Nov 29 2004
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Thomas wrote:

* For some reason, DMD on Linux returns a "12" ???
If you play a little around, you'll notice that the "return" value is pseudo random. I guess that whatever value is stored in EAX will be "returned" to the shell.
You are correct. (with DMD compiler)
 $ cat returncode.d
 void main() { asm { mov EAX, 42; } }
returns
 $ ./returncode; echo $?
 42
Still returns 1 with gdc, though... I think it should either be an error, or return 0 always ? --anders
Nov 29 2004