{"id":6,"date":"2009-01-08T22:36:29","date_gmt":"2009-01-08T22:36:29","guid":{"rendered":"http:\/\/sanchitkarve.com\/blog\/?p=6"},"modified":"2009-01-08T22:36:29","modified_gmt":"2009-01-08T22:36:29","slug":"void-main-vs-int-main","status":"publish","type":"post","link":"https:\/\/www.sanchitkarve.com\/blog\/2009\/01\/08\/void-main-vs-int-main\/","title":{"rendered":"void main() v\/s int main()"},"content":{"rendered":"<p>Many people use <em>void main()<\/em> instead of <em>int main()<\/em> while writing C\/C++ programs, inspite of <em>void<\/em> not being accepted as a return type for main in the C\/C++ standard.<\/p>\n<p>I too used to do the same until some programmers on a programming forum told me a few years ago about the C++ Standard.<\/p>\n<p>But what does <em>void<\/em> do anyway? How is it different from return 0?<\/p>\n<p>I disassembled a few test programs to check what really happens under the hood. \u00a0It is interesting to know what happens when a program is executed.<\/p>\n<p>Almost everybody believes that main()\/WinMain() is the entrypoint of all C\/C++ programs but it isn&#8217;t so.<\/p>\n<p>On Windows, a <strong>start()<\/strong> function gets called before the main() function. It first calls <em>GetVersion(), GetCommandLine(), GetEnvironmentStrings(), GetStartupInfo() and GetModuleHandle()<\/em> API Functions from kernel32.dll in the exact order. Then it passes the Module Handle, command-line arguments and environment variables as arguments to main() and then calls it.<\/p>\n<p>But what about main()&#8217;s return value? Is it read after main() quits?<\/p>\n<p>It doesn\u2019t matter what data type you return from main(). Whether its int or void it doesn\u2019t make any difference.<br \/>\nHere\u2019s what actually happens.<br \/>\nIn an executable file, the start() function which calls the main() function, expects a return value of data type integer so that it may decide what argument is passed to the exit() function which is called after main().<br \/>\nIf void is chosen as the return type for main, the EAX register is passed as an argument to the exit function.<br \/>\nSince the EAX register almost always contains the return value of any function, so if we say return 0, EAX would be set to zero, that\u2019s all.<br \/>\nGenerally whenever void is used, EAX is set to zero at the end of main() , so it is exactly the same as passing return 0.<br \/>\nBut since void main() is not a part of the standard, compiler developers are given full freedom to design their own implementation of the code after main() returns. Hence, we cannot always assume that EAX is set to 0 with a void main().<\/p>\n<p>Ever wondered what happens to the return value after main() quits?<br \/>\nTake a look at this disassembled snippet of the start() function from a C\/C++ program.<\/p>\n<pre lang=\"asm\">call dword ptr [esi+18h] ; call main()\nadd esp, 0Ch\n; (4 x 3) 12 bytes are cleared from the stack to clear space\n; occupied by main()'s three arguments.\npush eax ; status for exit. Return value of main() is pushed\ncall _exit<\/pre>\n<p>So no matter what you do, even if you ignore the return value type, the compiler will still pass the contents of the EAX Register into the exit function. This could lead to some unwanted results, and hence it is better to return 0 (ie. EXIT_SUCCESS) or 1(EXIT_FAILURE) depending on when and why you have designed the program to end.<\/p>\n<p>So even though, it is almost always the same, there is a reason why the standard recommends using int. Simply for the following reasons:<br \/>\nreturning 0 by default is COMPILER DEPENDENT.<br \/>\nNo Guarantee about the content of the EAX Register after main() exits.<br \/>\nWill not be portable to all Operating Systems.<br \/>\nMay cause incorrect termination of main()<\/p>\n<p>So even though void main and return 0 are alike, we should try to avoid its use.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many people use void main() instead of int main() while writing C\/C++ programs, inspite of void not being accepted as a return type for main in the C\/C++ standard. I too used to do the same until some programmers on a programming forum told me a few years ago about the C++ Standard. But what [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[21,22,32,60,71],"_links":{"self":[{"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/posts\/6"}],"collection":[{"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/comments?post=6"}],"version-history":[{"count":0,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/posts\/6\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/media?parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/categories?post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/tags?post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}