{"id":2339,"date":"2024-05-10T07:21:12","date_gmt":"2024-05-10T07:21:12","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2339"},"modified":"2024-05-10T07:21:12","modified_gmt":"2024-05-10T07:21:12","slug":"main-function-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/main-function-in-c\/","title":{"rendered":"Main Function In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#main-function-in-c\">Main Function In C<\/a><\/li><li ><a href=\"#syntax-of-c-main-function\">Important Points about C Main Function<\/a><\/li><li ><a href=\"#types-of-c-main-functions\">Types of C main Functions<\/a><ul><li ><a href=\"#1-main-function-with-no-arguments-and-void-return-type\">1. Main Function with No Arguments and Void Return Type<\/a><\/li><li ><a href=\"#2-main-function-with-no-arguments-and-int-return-type\"> 2. Main Function with No Arguments and int Return Type <\/a><\/li><li ><a href=\"#3-main-function-with-the-command-line-arguments\">3. Main Function with the Command Line Arguments<\/a><\/li><\/ul><\/li><li ><a href=\"#faq-main-function-in-c\">FAQ- Main Function In C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"main-function-in-c\">Main Function In C<\/h2>\n\n\n\n<p>In C, C++, and Java, the main function is crucial as it serves as the entry point of a program. When a program is executed, the operating system begins execution by running the statements within the main() function. It is a user-defined function and is mandatory for program execution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"syntax-of-c-main-function\">Syntax of C main() Function<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>return_type main() {\n    \/\/ Statement 1;\n    \/\/ Statement 2;\n    \/\/ and so on..\n    return;\n}<\/code><\/pre>\n\n\n\n<p>We can write the main function in many ways in&nbsp;<strong>C<\/strong>&nbsp;language as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int main(){} or int main(void){}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"important-points-about-c-main-function\">Important Points about C main Function<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The main function is where a program&#8217;s execution begins.<\/li>\n\n\n\n<li>Every program has exactly one main function.<\/li>\n\n\n\n<li>Its name should be &#8220;main,&#8221; not anything else.<\/li>\n\n\n\n<li>The main function typically returns an integer value or void.<\/li>\n\n\n\n<li>The main function is called by the operating system, not by the user, to start the program&#8217;s execution.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"types-of-c-main-functions\">Types of C main Functions<\/h2>\n\n\n\n<p><strong>Main Function with No Arguments and Void Return Type<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This is a standard form of the main function in some programming languages like C++.<\/li>\n\n\n\n<li>It doesn&#8217;t take any arguments.<\/li>\n\n\n\n<li>It doesn&#8217;t return a value (void).<\/li>\n<\/ul>\n\n\n\n<p><strong>Main Function with No Arguments and Int Return Type<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Another common variation of the main function in languages like C and C++.<\/li>\n\n\n\n<li>It doesn&#8217;t take any arguments.<\/li>\n\n\n\n<li>It returns an integer value.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-main-function-with-no-arguments-and-void-return-type\">1. Main Function with No Arguments and Void Return Type<\/h3>\n\n\n\n<p>It appears you want to see an example of a simple C program with the main function that prints a string using the <code>printf<\/code> function. Here&#8217;s an example in C:<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main()\n{\n   \/\/ Function body\n}<\/code><\/pre>\n\n\n\n<p>This given function is equivalent to<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main (void)\n{\n    \/\/ Function Body\n}<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to show main with no return type and no\n\/\/ arguments\n#include &lt;stdio.h&gt;\n \n\/\/ Defining a main function\nvoid main()\n{\n \n    \/\/ code\n    printf(\"Hello Skillvertex!\");\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello Skillvertex !<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-main-function-with-no-arguments-and-int-return-type\"><br>2. Main Function with No Arguments and int Return Type<br><\/h3>\n\n\n\n<p>In the example, the <code>int<\/code> return type in the <code>main()<\/code> function signifies the program&#8217;s exit status. By convention, a return value of 0 indicates successful completion, while any other value indicates an error during execution.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int main()\n{\n   \/\/ Function body\n}<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ C Program to show the main function with int return type\n\/\/ and no arguments\n#include &lt;stdio.h&gt;\n \nint main()\n{\n    printf(\"Hello Skillvertex!\");\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello Skillvertex!<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-main-function-with-the-command-line-arguments\">3. Main Function with the Command Line Arguments<\/h3>\n\n\n\n<p>In this example, we use command-line arguments in the <code>main()<\/code> function. <code>argc<\/code> stands for argument count and stores the number of arguments passed in the command line. By default, its value is 1 when no arguments are passed. <code>argv[]<\/code> is a char pointer array that stores all the command line arguments provided. When running the program without any command line arguments, <code>argc<\/code> is 1, indicating no additional arguments were passed.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int main(int argc, char* argv&#91;])\n{\n   \/\/ Function body\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to illustrate the main function with command line arguments\n#include &lt;stdio.h&gt;\n \nint main(int argc, char* argv)\n{\n \n    \/\/ printing the coundt of arguments\n    printf(\"The value of argc is %d\\n\", argc);\n    \/\/ prining each argument\n    for (int i = 0; i &lt; argc; i++) {\n        printf(\"%s \\n\", argv&#91;i]);\n    }\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The value of argc is 1\n.\/369df037-e886-4cfb-9fd4-ad6a358ad7c6<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-main-function-in-c\">FAQ- Main Function In C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1696507442706\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is the main function in the C example?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C, <code>main<\/code> is a predefined function and a special entry point for every C program. It is responsible for starting the program&#8217;s execution and terminating it. The <code>main<\/code> function has <code>int<\/code> or <code>void<\/code> as its return data type and is where the program code begins execution.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696507450281\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. How to use main () in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The main function in C is the program&#8217;s starting point and is executed by the operating system. It begins with an opening bracket &#8216;{&#8216; and ends with a closing bracket &#8216;}&#8217;. The main function can have a return type of &#8216;int&#8217; or &#8216;void&#8217;.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696507459229\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3.What are the 4 types of functions in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Function with no arguments and no return value.<br \/>Function with no arguments and a return value.<br \/>Function with arguments and no return value.<br \/>Function with arguments and with a return value.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Main Function In C In C, C++, and Java, the main function is crucial as it serves as the entry point of a program. When a program is executed, the operating system begins execution by running the statements within the main() function. It is a user-defined function and is mandatory for program execution. Syntax of &#8230; <a title=\"Main Function In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/main-function-in-c\/\" aria-label=\"More on Main Function In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5351,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[421],"class_list":["post-2339","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-main-function-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2339"}],"version-history":[{"count":13,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2339\/revisions"}],"predecessor-version":[{"id":10652,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2339\/revisions\/10652"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5351"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}