why do we use int main() instead of void main?

This is a discussion on why do we use int main() instead of void main? within the C++ Programming forums, part of the General Programming Boards category; hey guys,i wonder why we need to return an int in our programs ? what are the benefits of it! ...

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493

    why do we use int main() instead of void main?

    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)

    "...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.."
    Bill Bryson


  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by anon View Post
    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.
    you mean using void main() doesn't give us such feature?

    can you give me a simple example how a process utilizes this returned integer!

    tanx
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...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.."
    Bill Bryson


  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    you mean using void main() doesn't give us such feature?
    How can void main() return anything to the caller? After all, it is returning void...

    To get the return code of main() is dependent upon how you run it. For instance, from bash:

    Code:
    ./my_app
    echo $?

  5. #5
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by bithub View Post
    How can void main() return anything to the caller? After all, it is returning void...

    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)

    "...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.."
    Bill Bryson


  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    but i suppose void is nothing!
    correct
    how do we return or lets say swap nothing ! inside or between functions!?
    you don't.

    (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!?
    Correct, zero means success.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Masterx View Post
    hey guys,i wonder why we need to return an int in our programs ? what are the benefits of it!
    The benefit is that doing so is correct, and doing otherwise is wrong. End of story.

    "What are the benefits to obeying the rules?" Well for one, it means you're not BREAKING THE RULES.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,264
    Quote Originally Posted by Masterx View Post
    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!?
    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.
    Right 98% of the time, and don't care about the other 3%.

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    147
    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.

  10. #10
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Thank you all for your answers , really thanks
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...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.."
    Bill Bryson


  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,264
    Quote Originally Posted by JVene View Post
    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.
    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.

    Quote Originally Posted by JVene View Post
    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.
    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.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21