hey guys,i wonder why we need to return an int in our programs ? what are the benefits of it!
and if there is any benefit(s) why do we have void main() stuff!?
hey guys,i wonder why we need to return an int in our programs ? what are the benefits of it!
and if there is any benefit(s) why do we have void main() stuff!?
Last edited by Masterx; 04-22-2009 at 10:48 AM.
Highlight Your Codes
The Boost C++ Libraries (online Reference)
Bill Bryson"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
Because the latter is not standard and might result in a compile error (especially if you treat warnings as errors)? Because it is one character more to type?
The result can be used by the process that launched your program.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Highlight Your Codes
The Boost C++ Libraries (online Reference)
Bill Bryson"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
How can void main() return anything to the caller? After all, it is returning void...you mean using void main() doesn't give us such feature?
To get the return code of main() is dependent upon how you run it. For instance, from bash:
Code:./my_app echo $?
excuse me , im just a rooki, but i suppose void is nothing! how do we return or lets say swap nothing ! inside or between functions!?
and still i cant get itv how a typical application can make use of the return value ! (is it like that when the app which executed our program, recives a zero(our progs return value!), it understands that the program has sucessfully terminated!?and without it ( mentioning the int! ) it can not understand that!?)
Highlight Your Codes
The Boost C++ Libraries (online Reference)
Bill Bryson"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
correctbut i suppose void is nothing!
you don't.how do we return or lets say swap nothing ! inside or between functions!?
Correct, zero means success.(is it like that when the app which executed our program, recives a zero(our progs return value!), it understands that the program has sucessfully terminated!?
That's exactly the answer to your original question. The reason for int main() is to allow information to be passed back to the environment that ran the program. That environment (eg a windows command line, a unix shell script) can then take actions depending on results returned by the program.
That is not possible if the program has a void main().
Apart from that, void main() will not necessarily even compile. It is certainly invalid in C++, and the claimed support of it in 1989 C standard is an accident of wording more than anything else - which was fixed in the 1999 C standard.
I'd like to ad the origin of the return value.
In ancient times (the 70's and 80's), the 'original' operating system for which C was common used was Unix.
In Unix it was common to 'chain' operations, or to use shell scripts (a little like batch files) to call a series of command line utilities to perform operations.
Each of these utilities might be a compiled program, like your own.
Shell scripts might have some line that calls your utility, and then either chooses to stop processing, or continue to call another utility, knowing yours succeeded.
It may also be that you documented your utility such that it performs some work, then returns a value with meanings, like 0 means you're done, 1 means the results should now be processed by utility A, or return 2, meaning the results should be processed by utility B.
In a sense, it's as if the shell script calls your program, and main is the function it's calling, and therefore the return value of your main has meaning to the script just as if your main were a function in ITS instructions.
It may seem that "modern" GUI applications have no use for such return values, and they often do not bother. Still, the standard dictates the function signature, and although some compilers of old allowed void main to be used, it was non-standard, and therefore just plain wrong.
There are a few such arbitrary, and sometimes vestigial concepts in the language, which make more sense in historic context, which are maintained to this day because changing them would break existing code.
Thank you all for your answers , really thanks
Highlight Your Codes
The Boost C++ Libraries (online Reference)
Bill Bryson"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
You express this like Unix is an anachronism that no longer exists. Linux (several squillion variants), the BSD operating systems (FreeBSD, OpenBSD, and NetBSD), Solaris, Darwin (a BSD derivative that underpins MacOS) are all in common use. And all are Unix.
A significant number of forum sites (potentially this one) are hosted on some linux or BSD operating system.
GUI applications tend to loop forever, until a user tells them to exit normally. That is typically not an error condition, so few programmers of GUI applications need to use a return value. That's not the same as saying the capability doesn't exist or is never used.
Under the hood, most GUI libraries or frameworks (eg win32 under windows) have an entry point that returns some integer value. Operating system utilities can, and often do, retrieve that return value.