Advertisement

Programming Practice

Started by April 23, 2001 07:17 AM
4 comments, last by Flanders 23 years, 9 months ago
Hi there, I saw this under one of rodney''s threads, and instead of actually trying to get a word in there I thought I''d save my breath and try it here Somebody mentioned that the standard practice was to use: int main(...) instead of: void main(...) - My question is why? I can see that being able to use return(0) or something at any point would be usefull, but otherwise I see no reason why this is important or why it is the standard? Also, I''m assuming this is a C++ thing? In Java I''ve never really seen it done (although my experience is limited...) Anyway, input is appretiated Thanks
- Flanders -
While I''m sure theres a whole slew of technical answers to this question.. I think you answered the most basic one yourself. It''s useful to be able to return different numbers to see how your program exited for dealing with exceptions and errors.. is there an advantage to NOT using int main()?

-repp
Newwwbie :)You don't make friends with salad!
Advertisement
remember the good ol''choice.com file from DOS?
well if no then bahh

ok it makes a menu with the help of a .batch file
the trick is that the choice.com returns an INT to dos
and then the .bat file knows which key you pressed

let me show ya:

echo 1.dir
echo 2.clearscreen
choice [1-2]
// i don''t remember the syntax of choice command line!
if errorlevel==1 goto 1:
if errorlevel==2 goto 2:
1:
dir
goto end
2:
cls
end:

i hope you got one of the ideas why to use the INT MAIN thingy
besides you can check how your program existed so it''s real helpful!


Arkon
http://qsoft.cjb.net
There are two forms of main defined by the language standard:

int main();
int main(int argc, char** argv);

The form with void return is not standard. However, considering it''s been available in nearly every compiler for years, it''s become a common-use standard form.

The usefulenss of a return value on main is limited in a Windows/Mac environment, where command-line operations are minimal and most programs are visual in nature.

If you work in a *nix/BSD type environment, or use something like cygwin on Windows, then the return value from main is incredibly useful as a status message to calling programs. You can indicate failure or success of a program and chain things together; almost necessary if you use a tool such as ''make''.
Dos expects programs to return a int.

If they don''t ... weird shit can happen

heh, actually I don''t think dos actually cares anymore.

but it is standard practice that main() returns a int... so do it
Java actually has an equivalent. When you call System.exit( anInt ), anInt will be returned to the calling shell.

This topic is closed to new replies.

Advertisement