main Function in C - GeeksforGeeks

main Function in C

Last Updated : 22 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The main function is an integral part of the programming languages such as C, C++, and Java. The main function in C is the entry point of a program where the execution of a program starts. It is a user-defined function that is mandatory for the execution of a program because when a C program is executed, the operating system starts executing the statements in the main() function.

Syntax of C main() Function

return_type main() {
    // Statement 1;
    // Statement 2;
    // and so on..
    return;
}

We can write the main function in many ways in C language as follows:

int main(){} or int main(void){}
main(){} or void main(){} or main(void){} or void main(void){}

In the above notations, int means integer return type, and void return type means that does not return any information, or (void) or () means that does not take any information.

Important Points about C main Function

  • It is the function where the program’s execution starts.
  • Every program has exactly one main function.
  • The name of this function should be “main” not anything else.
  • The main function always returns an integer value or void.
  • The main function is called by OS, not the user.

Types of C main Functions

  1. Main function with no arguments and void return type
  2. Main function with no arguments and int return type
  3. Main function with the Command Line Arguments

1. Main Function with No Arguments and Void Return Type

Let’s see the example of the main function inside which we have written a simple program for printing a string. In the below code, first, we include a header file and then we define a main function inside which we write a statement to print a string using printf() function. The main function is called by the operating system itself and then executes all statements inside this function.

Syntax

void main()
{
   // Function body
}

The above function is equivalent to:

void main (void)
{
    // Function Body
}

Example

C




// C Program to show main with no return type and no
// arguments
#include <stdio.h>
 
// Defining a main function
void main()
{
 
    // code
    printf("Hello Geek!");
}


Output

Hello Geek!

Note: The return type of main function according to C standard should be int only. Even if your compiler is able to run void main(), it is recommended to avoid it.

2. Main Function with No Arguments and int Return Type

In this example, we use the int return type in the main() function that indicates the exit status of the program. The exit status is a way for the program to communicate to the operating system whether the program was executed successfully or not. The convention is that a return value of 0 indicates that the program was completed successfully, while any other value indicates that an error occurred.

Syntax

int main()
{
   // Function body
}

or

int main(void)
{
    // Function Body
}

Example

C




// C Program to show the main function with int return type
// and no arguments
#include <stdio.h>
 
int main()
{
    printf("Hello Geek!");
    return 0;
}


Output

Hello Geek!

3. Main Function with the Command Line Arguments

In this example, we have passed some arguments in the main() function as seen in the below code. These arguments are called command line arguments and these are given at the time of executing a program.

The first argument argc means argument count which means it stores the number of arguments passed in the command line and by default, its value is 1 when no argument is passed.

The second argument is a char pointer array argv[] which stores all the command line arguments passed. We can also see in the output when we run the program without passing any command line argument the value of argc is 1.

Syntax

int main(int argc, char* argv[])
{
   // Function body
}

Example

C




// C Program to illustrate the main function with command line arguments
#include <stdio.h>
 
int main(int argc, char* argv)
{
 
    // printing the coundt of arguments
    printf("The value of argc is %d\n", argc);
    // prining each argument
    for (int i = 0; i < argc; i++) {
        printf("%s \n", argv[i]);
    }
 
    return 0;
}


Output

The value of argc is 1
./369df037-e886-4cfb-9fd4-ad6a358ad7c6 

Now, run the programs in the command prompt or terminal as seen below screenshot and passed any arguments. main.exe is the name of the executable file created when the program runs for the first time. We passed three arguments “geeks for geeks” and print them using a loop.

output of main with command line arguments

Output of Main with Command Line Arguments



Previous Article
Next Article

Similar Reads

C/C++ program for calling main() in main()
Given a number N, the task is to write C/C++ program to print the number from N to 1 by calling the main() function using recursion.Examples: Input: N = 10 Output: 10 9 8 7 6 5 4 3 2 1Input: N = 5 Output: 5 4 3 2 1 Approach: Use static variable to initialise the given number N.Print the number N and decrement it.Call the main() function recursively
2 min read
Is it fine to write void main() or main() in C/C++?
In C/C++ the default return type of the main function is int, i.e. main() will return an integer value by default. The return value of the main tells the status of the execution of the program. The main() function returns 0 after the successful execution of the program otherwise it returns a non-zero value. Using void main in C/C++ is considered no
3 min read
Difference between "int main()" and "int main(void)" in C/C++?
Note: This was true for older versions of C but has been changed in C11 (and newer versions). In newer versions, foo() is same as foo(void). Consider the following two definitions of main(). Definition 1: C/C++ Code int main() { /* */ return 0; } C/C++ Code int main() { /* */ return 0; } Definition 2: C/C++ Code int main(void) { /* */ return 0; } C
3 min read
Return Statement vs Exit() in main() in C++
The return statement in C++ is a keyword used to return the program control from the called function to the calling function. On the other hand, the exit() function in C is a standard library function of &lt;stdlib.h&gt; that is used to terminate the process explicitly. The operation of the two may look different but in the case of the main() funct
3 min read
Print 1 to 100 without loop using Goto and Recursive-main
Our task is to print all numbers from 1 to 100 without using a loop. There are many ways to print numbers from 1 to 100 without using a loop. Two of them are the goto statement and the recursive main. Print numbers from 1 to 100 Using Goto statement Follow the steps mentioned below to implement the goto statement: declare variable i of value 0decla
5 min read
What does main() return in C and C++?
C According to coding standards, a good return program must exit the main function with 0. Although we are using void main() in C, In which we have not suppose to write any kind of return statement but that doesn't mean that C code doesn't require 0 as exit code. Let's see one example to clear our thinking about need of return 0 statement in our co
3 min read
Functions that are executed before and after main() in C
With GCC family of C compilers, we can mark some functions to execute before and after main(). So some startup code can be executed before main() starts, and some cleanup code can be executed after main() ends. For example, in the following program, myStartupFun() is called before main() and myCleanupFun() is called after main(). #include&lt;stdio.
1 min read
How to print "GeeksforGeeks" with empty main() in C, C++ and Java?
Write a program that prints "GeeksforGeeks" with empty main() function. You are not allowed to write anything in main(). C language One way of doing this is to apply GCC constructor attribute to a function so that it executes before main() (See this for details). #include &lt;stdio.h&gt; /* Apply the constructor attribute to myStartupFun() so that
2 min read
How to Change the Output of printf() in main() in C?
To change the output of printf() in main(), we can use Macro Arguments. #define macro can be used for this task. This macro is defined inside the function. Although, #define can be used without declaring it in the function, in that case always the printf() will be changed. The function needs to be called first to change the output of printf() in ma
2 min read
Can main() be overloaded in C++?
Predict the output of following C++ program. #include &lt;iostream&gt; using namespace std; int main(int a) { cout &lt;&lt; a &lt;&lt; &quot;\n&quot;; return 0; } int main(char *a) { cout &lt;&lt; a &lt;&lt; endl; return 0; } int main(int a, int b) { cout &lt;&lt; a &lt;&lt; &quot; &quot; &lt;&lt; b; return 0; } int main() { main(3); main(&quot;C++
2 min read
Article Tags :