Archived

This topic is now archived and is closed to further replies.

Flanders

Programming Practice

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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''.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites