Note that there are some explanatory texts on larger screens.

plurals
  1. POIs main() really start of a C++ program?
    primarykey
    data
    text
    <p>The section $3.6.1/1 from the C++ Standard reads,</p> <blockquote> <p>A program shall contain a global function called <strong>main</strong>, which is the designated <strong>start</strong> of the program.</p> </blockquote> <p>Now consider this code,</p> <pre><code>int square(int i) { return i*i; } int user_main() { for ( int i = 0 ; i &lt; 10 ; ++i ) std::cout &lt;&lt; square(i) &lt;&lt; endl; return 0; } int main_ret= user_main(); int main() { return main_ret; } </code></pre> <p>This sample code does what I intend it to do, i.e printing the square of integers from 0 to 9, <strong>before</strong> entering into the <code>main()</code> function which is supposed to be the "start" of the program.</p> <p>Have a look at the output here : <a href="http://www.ideone.com/Niy0R" rel="noreferrer">http://www.ideone.com/Niy0R</a></p> <p>I also compiled it with <code>-pedantic</code> option, GCC 4.5.0. It gives no error, not even warning!</p> <p>So my question is, </p> <p><strong>Is this code really Standard conformant?</strong></p> <p>If it's standard conformant, then does it not invalidate what the Standard says? <code>main()</code> is not start of this program! <code>user_main()</code> executed before the <code>main()</code>. </p> <p>I understand that to initialize the global variable <code>main_ret</code>, the <code>use_main()</code> executes first but that is a different thing altogether; the point is that, it <strong>does</strong> invalidate the quoted statement $3.6.1/1 from the Standard, as <code>main()</code> is NOT the <em>start</em> of the program; it is in fact the <strong>end</strong> of <em>this</em> program!</p> <hr> <p>EDIT:</p> <h2>How do you define the word 'start'?</h2> <p>It boils down to the definition of the phrase <em>"start of the program"</em>. So how exactly do you define it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload