int main() or void main() ??!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 2
Reputation: astrojith is an unknown quantity at this point 
Solved Threads: 0
astrojith astrojith is offline Offline
Newbie Poster

int main() or void main() ??!!

 
0
  #1
Nov 9th, 2006
Well, first of all, I'm astrojith. I'm new here at Daniweb. So, hello all of you :cheesy: !! . Now, to my question. In school, I'm being taught programs which all use "void main()". But, I've seen a lot of code using int main(). I asked my teacher about this but she has no idea what she's teaching. She told me "Because its the way it should be done " . I said(to myself), " What the heck !? " Without any reason ? There should be a reason for everything. So, can anybody tell me ?

Well, I've one more question. I'm presently using Turbo C++ IDE to code. I tried using Dev-C++ yesterday and I found out that it doesnt even accept void main() and that it doesnt like having ".h" in the end of header files as in <iostream.h>. I figured out why "using namespace std" is used. But, I cant figure out the other doubts. Pls help me.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,047
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: int main() or void main() ??!!

 
1
  #2
Nov 9th, 2006
It has to do with the C++ standard. The standard says, "use int", so that's what you should use. The reason you should use int is, void main is not necessarily accepted by every compiler.
Last edited by Rashakil Fol; Nov 9th, 2006 at 4:00 pm.
All my posts may be redistributed under the GNU Free Documentation License.
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: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: int main() or void main() ??!!

 
0
  #3
Nov 9th, 2006
> but she has no idea what she's teaching.
We get that a lot.

http://faq.cprogramming.com/cgi-bin/...&id=1043284376


It's only crappy DOS compilers which ever had this, presumably because DOS was so crappy at dealing with the return value that nobody ever cared.

All real compilers only accept int main, which is the only standard you should be worrying about.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: int main() or void main() ??!!

 
0
  #4
Nov 9th, 2006
Originally Posted by astrojith View Post
that it doesnt like having ".h" in the end of header files as in <iostream.h>. I figured out why "using namespace std" is used.
Your teacher is teaching such old and outdated coding styles. iostream replaced iostream.h like 10 years ago, and since then, you should either use "using namespace std;" or prefix any standard template objects with std::.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 115
Reputation: chunkmartinez is an unknown quantity at this point 
Solved Threads: 0
chunkmartinez's Avatar
chunkmartinez chunkmartinez is offline Offline
Junior Poster

Re: int main() or void main() ??!!

 
0
  #5
Nov 9th, 2006
Using int for a function isually means it returns an integer value.
Using void usually means it wont return an integer value.
MARTINEX();
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
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: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: int main() or void main() ??!!

 
0
  #6
Nov 9th, 2006
Originally Posted by chunkmartinez View Post
Using void usually means it wont return an integer value.
No -- main() will return an int whether you declare it void or not. If you declare it void then main() will return some random unpredictable integer.
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: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: int main() or void main() ??!!

 
0
  #7
Nov 10th, 2006
Originally Posted by Salem View Post
It's only crappy DOS compilers which ever had this,
What about crappy MSV-whatever? It allows void main() without a problem to this day, doesn't it? And OS/2's compiler didn't complain, either, if memory serves.

Originally Posted by Salem View Post
...presumably because DOS was so crappy at dealing with the return value that nobody ever cared.
I cared -- and used the return value all the time in .BAT files.

Originally Posted by Salem View Post
All real compilers only accept int main, which is the only standard you should be worrying about.
Absolutely... Anyone tells you different, smile knowingly, and deck them! Then have them talk to Salem....
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 2
Reputation: astrojith is an unknown quantity at this point 
Solved Threads: 0
astrojith astrojith is offline Offline
Newbie Poster

Re: int main() or void main() ??!!

 
0
  #8
Nov 10th, 2006
Oh, and I thought they were the same. That teacher doesnt know what sh's teaching. But, well, no other choice I guess. Thank y'all for telling me this.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 57
Reputation: may4life is an unknown quantity at this point 
Solved Threads: 2
may4life may4life is offline Offline
Junior Poster in Training

Re: int main() or void main() ??!!

 
0
  #9
Nov 10th, 2006
int main() is used to return 0 at the end. This 0 is returned to the Operating system to denote that the function main() - which is the only function called by the operating system and that's why its so special - has completed successfully. Always use int main() and return 0 at the end of it. Dont use void
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 115
Reputation: chunkmartinez is an unknown quantity at this point 
Solved Threads: 0
chunkmartinez's Avatar
chunkmartinez chunkmartinez is offline Offline
Junior Poster

Re: int main() or void main() ??!!

 
0
  #10
Nov 11th, 2006
Pretty mutch, just use Int main.
MARTINEX();
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
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC