void main returning int - why compiles?

Daren Scot Wilson darenw at darenscotwilson.com
Fri Dec 31 23:37:17 PST 2010


I'm wondering why the following compiles.  I'm using LDC.  Perhaps it's a 
bug, or there's some subtlety about D.   I have deliberately, out of a 
combination of idleness and desire for mischief, have  main()  declared as 
returning void, but with a return statement giving an integer.

If the first "half evil" return statement is uncommented, the corruption is 
noticed by the compiler and it writes an error.

As shown, the "total evil" return statement gets a value from subroutine 
foo().  Being somehow so perfect in its evilness, this passes through the 
compiler without a burp.  The resulting executable returns zero (or my bash 
shell defaults to zero when receiving nothing.)

When I get religion and like good boy declare main() as returing int, it 
compiles in perfectly.  When executed, the program returns either number 
according to which return statement is uncommented.




int foo(int x)   {
	return x;
}

void main()   {
	// return 333;   /* half evil */
	return foo(666);  /* total evil */
}



More information about the Digitalmars-d-learn mailing list