g++ and int main

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2004
Posts: 151
Reputation: Geek-Master is an unknown quantity at this point 
Solved Threads: 6
Geek-Master's Avatar
Geek-Master Geek-Master is offline Offline
Junior Poster

g++ and int main

 
0
  #1
Sep 16th, 2007
I am running Ubuntu and compiling my C++ code using g++. For some reason I can't get it to compile my source code using VOID MAIN. It is always telling me that I must use INT instead. This isn't life threatening, but I was just wondering if g++ just doesn't support a main function with a return of void. Anyone run into this problem?

GM
If in doubt, reach into the trash can and remove the user guide.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: g++ and int main

 
2
  #2
Sep 16th, 2007
The C++ standard says main is supposed to return int, right? I think that's the case, anyway.

So why would g++ support a main function with a return type of void? That makes no sense.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 151
Reputation: Geek-Master is an unknown quantity at this point 
Solved Threads: 6
Geek-Master's Avatar
Geek-Master Geek-Master is offline Offline
Junior Poster

Re: g++ and int main

 
0
  #3
Sep 17th, 2007
When you do use main to return an integer, that number that is sent back to the OS, how is it used? I know it is on the lines of error handling, 0 for no-errors and not 0 for errors.
If in doubt, reach into the trash can and remove the user guide.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: g++ and int main

 
1
  #4
Sep 17th, 2007
Yeah, the problem is you've been learning from people who never bothered to look at the standards. void main has never been a valid return type for main. Unfortunately for you, you've now got the pain of trying to unlearn something.

All compilers extend the language in some way, I guess you learnt with a compiler that blindly accepted void main without complaint. g++ is no different in that respect.
g++ -W -Wall -ansi -pedantic prog.cpp
does a pretty good job of reigning it back to being just an ANSI C++ compiler, where the code has a pretty good chance of being compiled with any other ANSI C++ compiler.


As for the return result, you do can something like this in say a bash script
  1. ./myprog
  2. if [ $? == 0 ]; then
  3. echo success
  4. fi
The shell variable $? contains the return value of the main in the program you most recently ran.

You can also get the return status in your C code if you want as well
  1. pid = fork();
  2. if ( pid == 0 ) {
  3. /* run your other program */
  4. execl("./prog", "./prog", (char*)NULL );
  5. } else if ( pid != -1 ) {
  6. int status;
  7. wait( &status );
  8. /* when prog exits, the return result is in status (along with some other stuff) */
  9. }
Use the various 'W' macros to find out whether it was a normal exit (and thus a valid return status), or whether the program was killed or crashed.

Finally, SOME implementations of system("./prog") also return the status, but this is far from certain.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 151
Reputation: Geek-Master is an unknown quantity at this point 
Solved Threads: 6
Geek-Master's Avatar
Geek-Master Geek-Master is offline Offline
Junior Poster

Re: g++ and int main

 
0
  #5
Sep 17th, 2007
So the return is mainly used for automation of individual programs in a batch or bash script. I've not focused much on C++ since I work in a Windows environment, but I can use that bit of info Salem.
If in doubt, reach into the trash can and remove the user guide.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,660
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: g++ and int main

 
0
  #6
Sep 17th, 2007
It is also used by programs when executing other programs. The return value tells the calling program whether it successfully executed its job or not. The return value is useful in a lot of situations, even in Windows programs.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 38
Reputation: spankyg is an unknown quantity at this point 
Solved Threads: 0
spankyg's Avatar
spankyg spankyg is offline Offline
Light Poster

Re: g++ and int main

 
0
  #7
Sep 18th, 2007
Void Main never has worked in the last who knows how many years! It violates C+ standards right from the start. There are a lot of text book examples using "void" forget about them!
Education is what remains after one has forgotten what one has learned in school.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 1731 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC