<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: int main() vs void main()</title>
	<atom:link href="http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html/feed" rel="self" type="application/rss+xml" />
	<link>http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html</link>
	<description>Making Your Code Faster, Stronger, Safer…</description>
	<lastBuildDate>Thu, 18 Feb 2010 05:11:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: خانواده گنو/لینوکس ایرانیان &#187; Blog Archive &#187; int main() در مقابل void main() تفاوت در چیست!؟</title>
		<link>http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html#comment-2088</link>
		<dc:creator>خانواده گنو/لینوکس ایرانیان &#187; Blog Archive &#187; int main() در مقابل void main() تفاوت در چیست!؟</dc:creator>
		<pubDate>Mon, 18 May 2009 19:56:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=4#comment-2088</guid>
		<description>[...] با این مطلب مواجه شدم، که به موضوع مهم و جالبی اشاره کرده بود! (که [...]</description>
		<content:encoded><![CDATA[<p>[...] با این مطلب مواجه شدم، که به موضوع مهم و جالبی اشاره کرده بود! (که [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html#comment-1255</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Thu, 23 Apr 2009 21:16:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=4#comment-1255</guid>
		<description>Regardless of whether the return value is pushed on the stack or returned in a register, stack corruption doesn&#039;t occur because you declare a function like main to be void.  If that was the case then you would not be able to declare any function in C/C++ void.  The return type for the function tells the compiler what value to expect the function to return.  If that value is to be passed on the stack then the compiler knows to allocate space on the stack for that return value.  void would tell the compiler that it doesn&#039;t need to allocate space on the stack for the return value.   Now if the calling system requires that a return value be present, then the compiler for that system should either provide the stub code for returning a default value to that system or flag void main() as a violation. However, it&#039;s ludicrous to expect compilers for other systems which either don&#039;t requre a return value or even use a return value from applications to flag void main() as a violation.  Why require extra code to be added just to satisfy cross-compliance with other systems especially when the app is not going to be used on those systems?  Of course you can have the compiler for that system strip out the unnecessary code, but why write the code in the first place.  If I&#039;m writing a word processor app then it doesn&#039;t need to run on embedded controler in an automobile.  I doubt fuel injection system needs to write a dear Abbey letter.</description>
		<content:encoded><![CDATA[<p>Regardless of whether the return value is pushed on the stack or returned in a register, stack corruption doesn&#8217;t occur because you declare a function like main to be void.  If that was the case then you would not be able to declare any function in C/C++ void.  The return type for the function tells the compiler what value to expect the function to return.  If that value is to be passed on the stack then the compiler knows to allocate space on the stack for that return value.  void would tell the compiler that it doesn&#8217;t need to allocate space on the stack for the return value.   Now if the calling system requires that a return value be present, then the compiler for that system should either provide the stub code for returning a default value to that system or flag void main() as a violation. However, it&#8217;s ludicrous to expect compilers for other systems which either don&#8217;t requre a return value or even use a return value from applications to flag void main() as a violation.  Why require extra code to be added just to satisfy cross-compliance with other systems especially when the app is not going to be used on those systems?  Of course you can have the compiler for that system strip out the unnecessary code, but why write the code in the first place.  If I&#8217;m writing a word processor app then it doesn&#8217;t need to run on embedded controler in an automobile.  I doubt fuel injection system needs to write a dear Abbey letter.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alexwebmaster</title>
		<link>http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html#comment-240</link>
		<dc:creator>Alexwebmaster</dc:creator>
		<pubDate>Tue, 03 Mar 2009 09:21:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=4#comment-240</guid>
		<description>Hello webmaster 
I would like to share with you a link to your site 
write me here preonrelt@mail.ru</description>
		<content:encoded><![CDATA[<p>Hello webmaster<br />
I would like to share with you a link to your site<br />
write me here <a href="mailto:preonrelt@mail.ru">preonrelt@mail.ru</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lundin</title>
		<link>http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html#comment-128</link>
		<dc:creator>Lundin</dc:creator>
		<pubDate>Mon, 09 Feb 2009 15:54:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=4#comment-128</guid>
		<description>The debate above about whether return values are pushed on the stack or not is clearly written by narrowminded Windows/desktop programmers. Yes, return values -are- saved on the stack in some cases. What Windows programmers don&#039;t know is that there are thousands of different embedded processors out there, all with different architectures. 90% of the computers in the world are not desktop computers. The computers controlling your car saves return values on the stack.

The article is about the C/C++ language, not about disassembled Windows code. How things are done in Windows may be interesting to know, but not relevant to a C/C++ debate.</description>
		<content:encoded><![CDATA[<p>The debate above about whether return values are pushed on the stack or not is clearly written by narrowminded Windows/desktop programmers. Yes, return values -are- saved on the stack in some cases. What Windows programmers don&#8217;t know is that there are thousands of different embedded processors out there, all with different architectures. 90% of the computers in the world are not desktop computers. The computers controlling your car saves return values on the stack.</p>
<p>The article is about the C/C++ language, not about disassembled Windows code. How things are done in Windows may be interesting to know, but not relevant to a C/C++ debate.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lundin</title>
		<link>http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html#comment-127</link>
		<dc:creator>Lundin</dc:creator>
		<pubDate>Mon, 09 Feb 2009 15:47:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=4#comment-127</guid>
		<description>This matter is rather simple, people make a fuss about it because they don&#039;t know the standards.

First, for your convenience I&#039;ll explain some terms used in both standards:

Hosted environment =  programming with an operating system (Windows, Linux, Unix, RTOS etc)
Freestanding environment = programming without an operating system, or programming the operating system itself.

We then have 4 cases:

C in a hosted environment
C in a freestanding environment
C++ in a hosted environment
C++ in a freestanding environment

The C++ standard chapter 3.6.1:

&quot;Main function

A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function.&quot; /--/

&quot;An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ } &quot;


The C standard:

&quot;5.1.2.1 Freestanding environment

In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined.&quot; 

/--/

&quot;5.1.2.2 Hosted environment&quot; /--/

&quot;The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent; or in some other implementation-defined manner.&quot;


The above is the absolute truth. You can&#039;t argue with the standard. You can however misinterpret it. In the C standard they end the statement with &quot;or in some implementation-defined manner.&quot;. This refers to the -arguments- of main, not the return type. I believe this was clearified in some errata, though I can&#039;t cite it. 

The C++ standard clearly states that:
In a freestanding environment, you don&#039;t need to declare main(), but if you do, it must return int. Now this doesn&#039;t make any sense as there is no underlaying OS to return the value to, yet the standard says so, so we must obey. In freestanding environments you never end the program, so the calling convention of main() isn&#039;t important. In a hosted environment, main() always returns int without exceptions.

The C standard states that:
In a freestanding environment, main() may return any type.
In a hosted environment, main() shall only return int.

In a hosted environment, the following is therefore not allowed in C/C++:

main()
void main()

nor are further variants like WinMain() legal C/C++. This has always been the case, the definition of main() has not been changed in any standard since the first ANSI/ISO C released in 1990. Early &quot;K&amp;R C&quot; might have allowed all kind of crazy stuff, but it was not standardized so it doesn&#039;t matter.

This means that main() and void main() will not compile on a C/C++ compiler made for hosted environments. There will be no runtime crashes, because the code will not even compile. If it compiles, the compiler does not follow ISO C/C++, ie it is not a C/C++ compiler but something else. If you run your C/C++ program on a &quot;something else compiler&quot;, yes then unexpected things are bound to happen. 

This also means that most Microsoft compilers aren&#039;t C/C++ compilers. Early Borland compilers are not C/C++ compilers. And so on.</description>
		<content:encoded><![CDATA[<p>This matter is rather simple, people make a fuss about it because they don&#8217;t know the standards.</p>
<p>First, for your convenience I&#8217;ll explain some terms used in both standards:</p>
<p>Hosted environment =  programming with an operating system (Windows, Linux, Unix, RTOS etc)<br />
Freestanding environment = programming without an operating system, or programming the operating system itself.</p>
<p>We then have 4 cases:</p>
<p>C in a hosted environment<br />
C in a freestanding environment<br />
C++ in a hosted environment<br />
C++ in a freestanding environment</p>
<p>The C++ standard chapter 3.6.1:</p>
<p>&#8220;Main function</p>
<p>A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function.&#8221; /&#8211;/</p>
<p>&#8220;An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:</p>
<p>int main() { /* &#8230; */ }<br />
and<br />
int main(int argc, char* argv[]) { /* &#8230; */ } &#8221;</p>
<p>The C standard:</p>
<p>&#8220;5.1.2.1 Freestanding environment</p>
<p>In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined.&#8221; </p>
<p>/&#8211;/</p>
<p>&#8220;5.1.2.2 Hosted environment&#8221; /&#8211;/</p>
<p>&#8220;The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:</p>
<p>int main(void) { /* &#8230; */ }</p>
<p>or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):</p>
<p>int main(int argc, char *argv[]) { /* &#8230; */ }</p>
<p>or equivalent; or in some other implementation-defined manner.&#8221;</p>
<p>The above is the absolute truth. You can&#8217;t argue with the standard. You can however misinterpret it. In the C standard they end the statement with &#8220;or in some implementation-defined manner.&#8221;. This refers to the -arguments- of main, not the return type. I believe this was clearified in some errata, though I can&#8217;t cite it. </p>
<p>The C++ standard clearly states that:<br />
In a freestanding environment, you don&#8217;t need to declare main(), but if you do, it must return int. Now this doesn&#8217;t make any sense as there is no underlaying OS to return the value to, yet the standard says so, so we must obey. In freestanding environments you never end the program, so the calling convention of main() isn&#8217;t important. In a hosted environment, main() always returns int without exceptions.</p>
<p>The C standard states that:<br />
In a freestanding environment, main() may return any type.<br />
In a hosted environment, main() shall only return int.</p>
<p>In a hosted environment, the following is therefore not allowed in C/C++:</p>
<p>main()<br />
void main()</p>
<p>nor are further variants like WinMain() legal C/C++. This has always been the case, the definition of main() has not been changed in any standard since the first ANSI/ISO C released in 1990. Early &#8220;K&amp;R C&#8221; might have allowed all kind of crazy stuff, but it was not standardized so it doesn&#8217;t matter.</p>
<p>This means that main() and void main() will not compile on a C/C++ compiler made for hosted environments. There will be no runtime crashes, because the code will not even compile. If it compiles, the compiler does not follow ISO C/C++, ie it is not a C/C++ compiler but something else. If you run your C/C++ program on a &#8220;something else compiler&#8221;, yes then unexpected things are bound to happen. </p>
<p>This also means that most Microsoft compilers aren&#8217;t C/C++ compilers. Early Borland compilers are not C/C++ compilers. And so on.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Slow Updates And A Small Nugget Post &#124; Shantanu's Technophilic Musings</title>
		<link>http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html#comment-59</link>
		<dc:creator>Slow Updates And A Small Nugget Post &#124; Shantanu's Technophilic Musings</dc:creator>
		<pubDate>Tue, 13 Jan 2009 04:50:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=4#comment-59</guid>
		<description>[...] take a look at this post ( int Main() vs void main() )that I had made some time ago at another blog of mine ( Safer Code - Secure Coding In C  C++ And [...]</description>
		<content:encoded><![CDATA[<p>[...] take a look at this post ( int Main() vs void main() )that I had made some time ago at another blog of mine ( Safer Code &#8211; Secure Coding In C  C++ And [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charles Zuppann</title>
		<link>http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html#comment-58</link>
		<dc:creator>Charles Zuppann</dc:creator>
		<pubDate>Mon, 12 Jan 2009 21:53:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=4#comment-58</guid>
		<description>It seems to me that this talk about registers is distracting from the point. Any compiler that allows you to write void main() and then produces code that can bomb is a bad compiler. Either it should complain if you don&#039;t have int main() or construct code that works if you have void main(). That being said, If we are going to allow void main() then all compilers should allow it - we should change the standard. If we change the standard to allow void main() then it should be handled consistently - perhaps meaning that main does not deliver any kind of exit status and that the compiler should take care of it. As to return values being in registers we should remember that there are other processors out there some of which do not have registers available for the purpose and which therefore have to return value on the stack. If you want to write truly portable code you should make no assumtions about how parameters or results are passed.</description>
		<content:encoded><![CDATA[<p>It seems to me that this talk about registers is distracting from the point. Any compiler that allows you to write void main() and then produces code that can bomb is a bad compiler. Either it should complain if you don&#8217;t have int main() or construct code that works if you have void main(). That being said, If we are going to allow void main() then all compilers should allow it &#8211; we should change the standard. If we change the standard to allow void main() then it should be handled consistently &#8211; perhaps meaning that main does not deliver any kind of exit status and that the compiler should take care of it. As to return values being in registers we should remember that there are other processors out there some of which do not have registers available for the purpose and which therefore have to return value on the stack. If you want to write truly portable code you should make no assumtions about how parameters or results are passed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jg</title>
		<link>http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html#comment-57</link>
		<dc:creator>jg</dc:creator>
		<pubDate>Mon, 12 Jan 2009 19:05:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=4#comment-57</guid>
		<description>You&#039;ve run one MS console mode command, and concluded that all software doesn&#039;t just return 0 regardless of failure? That is basing your conclusion upon the flimiest of evidence. Try running MS Word, and pass it the name of a non-existant file to load. See what you get back then.</description>
		<content:encoded><![CDATA[<p>You&#8217;ve run one MS console mode command, and concluded that all software doesn&#8217;t just return 0 regardless of failure? That is basing your conclusion upon the flimiest of evidence. Try running MS Word, and pass it the name of a non-existant file to load. See what you get back then.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Allen</title>
		<link>http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html#comment-56</link>
		<dc:creator>John Allen</dc:creator>
		<pubDate>Mon, 12 Jan 2009 14:32:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=4#comment-56</guid>
		<description>jd you are such an idiot.

try the following in a windows cmd shell
xcopy
echo %ERRORLEVEL%

xcopy c:\autoexec.bat c:\autoexec.tmp
echo %ERRORLEVEL%

see how the first echo prints 4, and the second one 0. Even windows programs return 0 for successful completion, and this behaviour was taken from Unix, and still holds true today. Windows entry point WinMain, is declared as int WinMain().</description>
		<content:encoded><![CDATA[<p>jd you are such an idiot.</p>
<p>try the following in a windows cmd shell<br />
xcopy<br />
echo %ERRORLEVEL%</p>
<p>xcopy c:\autoexec.bat c:\autoexec.tmp<br />
echo %ERRORLEVEL%</p>
<p>see how the first echo prints 4, and the second one 0. Even windows programs return 0 for successful completion, and this behaviour was taken from Unix, and still holds true today. Windows entry point WinMain, is declared as int WinMain().</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: int main() در مقابل void main() تفاوت در چیست!؟ &#171; MGroup</title>
		<link>http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html#comment-55</link>
		<dc:creator>int main() در مقابل void main() تفاوت در چیست!؟ &#171; MGroup</dc:creator>
		<pubDate>Mon, 12 Jan 2009 11:42:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=4#comment-55</guid>
		<description>[...] با این مطلب مواجه شدم، که به موضوع مهم و جالبی اشاره کرده بود! (که [...]</description>
		<content:encoded><![CDATA[<p>[...] با این مطلب مواجه شدم، که به موضوع مهم و جالبی اشاره کرده بود! (که [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
