Active TopicsActive Topics  Display List of Forum MembersMemberlist  CalendarCalendar  Search The ForumSearch  HelpHelp
  RegisterRegister  LoginLogin
 One Stop GATE ForumGATE Technical DiscussionsGATE CS
Message Icon Topic: query Post Reply Post New Topic
Author Message
Pallavi
Newbie
Newbie


Joined: 03Feb2007
Online Status: Offline
Posts: 1
Quote Pallavi Replybullet Topic: query
    Posted: 03Feb2007 at 3:10pm
why is int main() used when void main() is good enough?



Post Resume: Click here to Upload your Resume & Apply for Jobs

IP IP Logged
Preeti
Newbie
Newbie


Joined: 03Feb2007
Online Status: Offline
Posts: 9
Quote Preeti Replybullet Posted: 03Feb2007 at 4:24pm
Historically, when C was first specified, there was no void main(). main() by default was supposed to return an integer vaule and so is if we declare int main(). Actually at that time there were two diclarations, int main() and int main(int argc, char **argv).

Now suppose you start writing a program with main() or int main(). If the program does not have an exit() code in between it must return a value, otherwise the compiler issues a warning. Now as I said the ANSI/ISO C Standard specifies that main() should be declared as int. So when runtime environment assumes that main returns an int, declaring main with any other return type, including void, invites trouble. The compiler might compile such a program, since the ANSI Standard doesn't require it to fail, but the behavior of such a program is, in the Standard's parlance, "undefined". That's why GCC returns an error in case we use void main().

In C++, the situation is good. void main() use is explicitely prohibited there and in case you forget to return, the compiler adds a return 0 itself!

Void main() in C is actually introduced to bypass the "complexity" to return.
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum

GET LATEST FRESHERS JOBS IN YOUR MAIL





This page was generated in 0.203 seconds.
Vyom is an ISO 9001:2000 Certified Organization

© Vyom Technosoft Pvt. Ltd. All Rights Reserved.

Job Interview Questions | Girls Magazine | DLL, OCX File Errors | Freshers Jobs | Placement Papers | More Papers