What is the proper declaration of main in C++?

C++ProgrammingServer Side Programming

<p>The main() function is a global function. It is used to start the execution of program. Every program should have main(). The command line arguments argc and argv are optional.</p><p>The standard prototype of main() function is as follows.</p><pre class="result notranslate">int main() { body } OR int main(int argc, char *argv[]) { body }</pre><p>Here,</p><p><strong>argc</strong> &minus; Number of arguments passed to the program from the environment where program runs.</p><p><strong>argv</strong> &minus; pointer to the first element of an array.</p><p>The following is an example of main()</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/4IhJ75" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate" style="">#include &lt;iostream&gt; using namespace std; int sum(int x, int y) { &nbsp; &nbsp;int s = x + y; &nbsp; &nbsp;cout &lt;&lt; &quot;The sum of numbers : &quot; &lt;&lt; s; } int main() { &nbsp; &nbsp;sum(28, 8); &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><pre class="result notranslate">The sum of numbers : 36</pre><p>In the above program, the code is present in sum() to calculate the sum of two numbers.</p><pre class="result notranslate" style="">int sum(int x, int y) { &nbsp; &nbsp;int s = x + y; &nbsp; &nbsp;cout &lt;&lt; &quot;The sum of numbers : &quot; &lt;&lt; s; }</pre><p>The main() function is invoking function sum()</p><pre class="result notranslate">sum(28, 8);</pre>
Updated on 26-Jun-2020 09:05:26