The Wayback Machine - https://web.archive.org/web/20230512155036/https://www.geeksforgeeks.org/main-function-in-c/
Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

main Function in C

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The main function is an integral part of most programming languages such as C, C++, and Java. This function is the entry level 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 program at the main() function.

Syntax of the main function in C:

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.

Key points about the 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 Main Function

  1. Main function with void return type
  2. Main function with int return type
  3. Main function with the argument
  4. Main function with more than one argument and int return type
  5. Main function with more than one argument and void return type

1. Main function with 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.

C




#include <stdio.h>
  
// Defining a main function
void main()
{
  
    // code
    printf("Hello Geek!");
}

Output

Hello Geek!

2. Main function with 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.

C




#include <stdio.h>
  
int main()
{
    printf("Hello Geek!");
    return 0;
}

Output

Hello Geek!

3. Main function with the argument

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.

C




#include <stdio.h>
  
int main(int argc, char* argv[])
{
  
    printf("The value of argc is %d\n", argc);
  
    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.

Main Function in C Language

 

4. Main function with more than one argument and int return type

This function is similar to the main function with an int return type but this function indicates that the function doesn’t take any arguments. In this example, the main function is like the int main(void ) which shows that it returns an integer value without passing any arguments to this function.

C




#include <stdio.h>
  
int main(void)
{
  
    printf("Hi Geeks!\n");
    return 0;
}

Output

Hi Geeks!

5. Main function with more than one argument and void return type

This function is similar to the main function with a void return type but this function indicates that the function doesn’t take any arguments. In this example, we have defined the main function as the void main(void ) which shows that it doesn’t return any value and also we are not passing any arguments to this function.

C




#include <stdio.h>
  
void main(void) {
  
    printf("Hi Geeks!\n");
      
}

Output

Hi Geeks!

My Personal Notes arrow_drop_up
Last Updated : 05 Apr, 2023
Like Article
Save Article
Similar Reads