954,190 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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 . I figured out why "using namespace std" is used. But, I cant figure out the other doubts. Pls help me.

astrojith
Newbie Poster
2 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 176
 

> but she has no idea what she's teaching.
We get that a lot.

http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044841143&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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
that it doesnt like having ".h" in the end of header files as in . 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:: .

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

Using int for a function isually means it returns an integer value.
Using void usually means it wont return an integer value.

chunkmartinez
Junior Poster
115 posts since Jun 2006
Reputation Points: 14
Solved Threads: 0
 
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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 
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....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.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 toSalem.... :twisted:

WaltP
Posting Sage w/ dash of thyme
Moderator
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
 

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.

astrojith
Newbie Poster
2 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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

may4life
Junior Poster in Training
57 posts since Oct 2006
Reputation Points: 13
Solved Threads: 2
 

Pretty mutch, just use Int main.

chunkmartinez
Junior Poster
115 posts since Jun 2006
Reputation Points: 14
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You