{"id":3479,"date":"2024-05-28T04:43:42","date_gmt":"2024-05-28T04:43:42","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=3479"},"modified":"2024-05-28T04:43:43","modified_gmt":"2024-05-28T04:43:43","slug":"main-function","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/05\/28\/main-function\/","title":{"rendered":"Main Function"},"content":{"rendered":"\n<p>In a C program, the&nbsp;<strong><em>main()<\/em>&nbsp;function<\/strong>&nbsp;is the entry point. The program execution starts with the&nbsp;<strong><em>main()<\/em>&nbsp;function<\/strong>. It&nbsp;is designed to perform the main processing of the program and clean up any resources that were allocated by the program. In a C code, there may be any number of functions, but it must have a&nbsp;<strong><em>main()<\/em>&nbsp;function<\/strong>. Irrespective of its place in the code, it is the first function to be executed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of C main() Function<\/h2>\n\n\n\n<p>The following is the syntax of the\u00a0<strong>main()<\/strong>\u00a0function in\u00a0C language\u00a0\u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>intmain(){\/\/one or more statements;return0;}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax Explained<\/h3>\n\n\n\n<p>As a part of its syntax, a function has a name that follows the rules of forming an identifier (starting with an alphabet or underscore and having alphabet, digit or underscore). The name is followed by a parenthesis. Typically, the main() function is defined with no arguments, although it may have argv and argv argument to receive values from the command line.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Valid Signatures of C main() Function<\/h2>\n\n\n\n<p>The signatures (prototype) of a main() function may be \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>intmain(){..return0;}<\/code><\/pre>\n\n\n\n<p>Or<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>intmain(void){..return0;}<\/code><\/pre>\n\n\n\n<p>Or<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>intmain(int argc,char*argv&#91;]){..return0;}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of C main() Function<\/h2>\n\n\n\n<p>The following example demonstrates the main() function:<\/p>\n\n\n\n<pre id=\"0\" class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;intmain(){\/\/ Write code from hereprintf(\"Hello World\");return0;}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Important Points about C main() Function<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A C program must have a\u00a0<em>main()<\/em>\u00a0function.<\/li>\n\n\n\n<li>The main is not a\u00a0C keyword.<br>It is classified as a\u00a0user-defined function\u00a0because its body is not pre\u2212decided, it depends on the processing logic of the program.<br>By convention, int is the return type of\u00a0<em>main()<\/em>. The last statement in the function body of\u00a0<em>main()<\/em>\u00a0returns 0, to indicate that the function has been successfully executed. Any non\u2212zero return value indicates failure.<\/li>\n\n\n\n<li>Some old C compilers let you define\u00a0<em>main()<\/em>\u00a0function with void return type.<\/li>\n\n\n\n<li>However, this is considered to be non\u2212standard and is not recommended.<\/li>\n\n\n\n<li>As compared to other functions, the\u00a0<em>main()<\/em>\u00a0function:\n<ul class=\"wp-block-list\">\n<li>Can&#8217;t be declared as inline.<\/li>\n\n\n\n<li>Can&#8217;t be declared as static.<\/li>\n\n\n\n<li>Can&#8217;t have its address taken.<\/li>\n\n\n\n<li>Can&#8217;t be called from your program.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How does main() Works in C?<\/h2>\n\n\n\n<p>The program&#8217;s execution starts from the main() function as it is an entry point of the program, it starts executing the statements written inside it. Other functions within the source program are defined to perform certain task. The main function can call any of these functions. When main calls another function, it passes execution control to the function, optionally passing the requisite number and type of arguments, so that execution begins at the first statement in the called function. The called function returns control to main when a return statement is executed or when the end of the function is reached. Note that return statement is implicitly present as the last statement when its return type is int.<\/p>\n\n\n\n<p>A program usually stops executing when it returns from or reaches the end of main, although it can terminate at other points in the program for various reasons. For example, you may want to force the termination of your program when some error condition is detected. To do so, you can use the exit function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The exit() in C main() Function<\/h2>\n\n\n\n<p>The\u00a0C exit() function\u00a0is a standard library function used to terminate the calling process. Use exit(0) to indicate no error, and exit(1) to indicate that the program is exiting with because of an error encountered.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Using exit() in the main() Function<\/h3>\n\n\n\n<pre id=\"1\" class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;#include &lt;stdlib.h&gt;intadd(int,int);intmain(){int i;for( i =1; i&lt;=5; i++){if( i ==3){printf(\" \\n exiting ..\");exit(0);}elseprintf(\" \\n Number is %d\", i);}return0;}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Output<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Number is 1\nNumber is 2\nexiting ..\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Command-line Arguments with main() in C<\/h2>\n\n\n\n<p>Typically, the&nbsp;<strong>main()<\/strong>&nbsp;function is defined without any arguments. However, you may define main() with arguments to let it accept the values from the command line. In this type of usage, main() function is defined as follows \u2212<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<p>The following is the syntax of main() function with\u00a0command-line arguments:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>intmain(int argc,char*argv&#91;]){..return0;}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Argument Definitions<\/h3>\n\n\n\n<p>The argument definitions are as follows \u2212<\/p>\n\n\n\n<p><strong>argc&nbsp;<\/strong>\u2212 The first argument is an integer that contains the count of arguments that follow in argv. The argc parameter is always greater than or equal to 1.<\/p>\n\n\n\n<p><strong>argv<\/strong>&nbsp;\u2212 The second argument is an array of null\u2212terminated strings representing command-line arguments entered by the user of the program. By convention, argv[0] is the command with which the program is invoked. argv[1] is the first command\u2212line argument. The last argument from the command line is argv[argc \u2212 1], and argv[argc] is always NULL.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Using main() Function with Command-line Arguments<\/h3>\n\n\n\n<p>Consider the following program to understand command\u2212line arguments.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;#include &lt;stdlib.h&gt;intadd(int,int);intmain(int argc,char*argv&#91;]){int x, y, z;if(argc&lt;3){printf(\"insufficient arguments\");}else{\n      x =atoi(argv&#91;1]);\n      y =atoi(argv&#91;2]);\n      z = x+y;printf(\"addition : %d\", z);}return0;}<\/code><\/pre>\n\n\n\n<p>Just compile and build the program as test.c, don\u2019t run from the IDE in which you have edited and compiled. Go to the command prompt and run the program as follows \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>C:\\Users\\mlath&gt;test 1020\naddition :30<\/code><\/pre>\n\n\n\n<p>In this chapter, we learned the importance and syntax of defining a main() function in C. Any C program must have a main() function. As a convention it should return 0 to indicate successful execution. You can also define arguments to a main() function, they can be passed from the command\u2212line.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a C program, the&nbsp;main()&nbsp;function&nbsp;is the entry point. The program execution starts with the&nbsp;main()&nbsp;function. It&nbsp;is designed to perform the main processing of the program and clean up any resources that were allocated by the program. In a C code, there may be any number of functions, but it must have a&nbsp;main()&nbsp;function. Irrespective of its place [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[248],"tags":[],"class_list":["post-3479","post","type-post","status-publish","format-standard","hentry","category-02-c"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/3479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=3479"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/3479\/revisions"}],"predecessor-version":[{"id":3484,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/3479\/revisions\/3484"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=3479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=3479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=3479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}