Mail Archives: djgpp/1997/06/12/19:27:27
Robert Humphris wrote:
>
> Man, virtually every book that I have ever read on 'how to program C/C++' has used
> void main() in its code... the fact that most commercial DOS compilers seem to accept it
> without complaint seems to me that its a DOS thing this void main(). DJGPP is ported
> from one of the better operating systems ( NOTE: I said better, not best ) where the value
> that a process returns is often important.
It can be important in DOS as well; there are lots of COMMAND.COM batch
files that read the exit code of a program to determine what to do. But
all that's required to prove that what I am saying is correct is to
compile the following program:
--- voidmain.c ---
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* functions return most values in eax, so retval from this func. will
* "fall into" retval for main()
*/
int return_something( void )
{
return random();
}
void main()
{
srandom( (int) time( NULL ) );
return_something();
}
--- end ---
Then run it several times with the following command:
redir -x voidmain
The output that results should convince anyone who is still skeptical
about the value of obeying the ANSI standard.
For C++ people: if you compile the above program as C++, and set gcc
for maximum pessimism by using the '-Wall' switch, you will see the
following warning:
voidmain.cc:14: warning: return type for `main' changed to integer type
This is the compiler complaining that you did it wrong and that it went
ahead and fixed it for you. Warnings should never be ignored.
BTW, the book I use, _The New C Primer Plus, 2nd edition_, by the Waite
Group, is a highly recommended beginners' C text and it ALWAYS uses an
ANSI correct definition of main(). I believe that K&R (the definitive
work on C) also uses the correct definition. Some authors (okay, many
authors) are just plain lazy, and that extends to C instructors and the
people who write compilers. None of this makes it correct.
--
---------------------------------------------------------------------
| John M. Aldrich | "Waking a person unnecessarily |
| aka Fighteer I | should not be considered a capital |
| mailto:fighteer AT cs DOT com | crime. For a first offense, that |
| http://www.cs.com/fighteer | is." - Lazarus Long |
---------------------------------------------------------------------
- Raw text -