![]() |
![]() ![]() ![]() ![]() ![]() |
![]() ![]() |
![]() |
![]() |
![]() ![]() |
Author | Message |
Pallavi
Newbie ![]() Joined: 03Feb2007 Online Status: Offline Posts: 1 |
![]() ![]() ![]() 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 |
|
![]() |
|
Preeti
Newbie ![]() Joined: 03Feb2007 Online Status: Offline Posts: 9 |
![]() ![]() ![]() |
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. |
|
![]() |
|
![]() ![]() |
||
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 |
|
© Vyom Technosoft Pvt. Ltd. All Rights Reserved.