943,683 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6510
  • C++ RSS
Nov 9th, 2006
0

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

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
astrojith is offline Offline
2 posts
since Nov 2006
Nov 9th, 2006
1

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

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.
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Nov 9th, 2006
0

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

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 9th, 2006
0

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

Click to Expand / Collapse  Quote originally posted by astrojith ...
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::.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Nov 9th, 2006
0

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

Using int for a function isually means it returns an integer value.
Using void usually means it wont return an integer value.
Reputation Points: 14
Solved Threads: 0
Junior Poster
chunkmartinez is offline Offline
115 posts
since Jun 2006
Nov 9th, 2006
0

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,949 posts
since Aug 2005
Nov 10th, 2006
0

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

Click to Expand / Collapse  Quote originally posted by Salem ...
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.

Click to Expand / Collapse  Quote originally posted by Salem ...
...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.

Click to Expand / Collapse  Quote originally posted by Salem ...
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....
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Nov 10th, 2006
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
astrojith is offline Offline
2 posts
since Nov 2006
Nov 10th, 2006
0

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

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
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
may4life is offline Offline
57 posts
since Oct 2006
Nov 11th, 2006
0

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

Pretty mutch, just use Int main.
Reputation Points: 14
Solved Threads: 0
Junior Poster
chunkmartinez is offline Offline
115 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: What's Wrong with my Array?
Next Thread in C++ Forum Timeline: Turbo C doubt





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC