What is the main in C?
In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the program. It is a particular function that always starts running code from the "main" function with a return data type of "int" or "void."
In other words, the main() function is the point at which the program code begins to execute.
Important aspects of the "main()" function
- When users or programmers run their code, the operating system (OS) always runs the "main()" function.
- The "main()" function is responsible for initiating and terminating the program.
- It is a widely recognized keyword in programming languages, and its meaning and nomenclature cannot be changed.
- The "main()" function is a user-specified method in the C programming language, which implies that we can supply arguments to it based on the needs of the program.
- The main() function can be used to activate programming code at runtime rather than at program compilation time.
- The "main()" function is accompanied by parenthesis brackets that open and close.
Syntax for “main()” function in the C programming language:
main(){
/* Statement -1 */
/* Statement -2 */
}
Example: 1
Consider a program in C that prints a statement without utilizing the int main() and void main() functions.
#include <stdio.h>
main()
{
printf(“Hello! Welcome to the C programming language”);
}
Output:

Example: 2
Using the main() method, let's create a program that calls nested functions.
#include <stdio.h>
main()
{
printf (" It is a main() function block. ");
int funct_2(); /* Go to the void funct_1() function. */
printf ("\n Finally, the main() function is exited.");
}
void funct_1()
{
printf (" It is funct_1() function. ");
printf (" Exit from the void fun1() function. ");
}
int funct_2()
{
void funct_1(); /* Go to the int funct_1() function */
printf (" It is funct_2() function. ");
printf (" Exit from the int funct_2() function. ");
return 0;
}
Output:

Types of the "main()" function in the C programming language
- int main() function
- void main() function
- int main(int argc , char ** argv)
- void main(void)
- int main(void)
1. int main() function: A keyword that refers to an integer data type is "int." When used with the main() function, the int data type specifies that the function must return an int (integer) value. When we utilize an int main() function, we must include a "return 0;" statement at the conclusion of the function. The "return 0;" statement indicates that the program was successfully executed; any other statement indicates that the program was terminated unsuccessfully.
Syntax for int main() function:
int main()
{
/* Statement-1 */
/* Executed Code */
return 0;
}
Example: 1
Let us consider a program in C that returns a value using the int main() method.
#include <stdio.h>
int main()
{
printf (" Hello! Good Day");
printf( " \n You are in int main() function block with int return type" );
return 0;
}
Output:

Example: 2
Consider the following example of displaying an iterative sequence of numbers in C using the int main() function:
#include <stdio.h>
int main()
{
static int num = 20;
if ( --num) {
printf (" %d ", num); // print the number
main(20);
}
return 0;
}
Output:

We may also use an "EXIT_SUCCESS" statement, which is the "return 0;" alternative statement. In the absence of a "return 1;" statement, we can use "EXIT_FAILURE" if the code is not properly performed. The return statement is specified in the input/output header file "stdio.h," whereas the "EXIT" statement is specified in the standard library header file "stdlib.h."
Program Code:
Let us consider a C program that replaces the return statement with the "EXIT_SUCCESS" statement.
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf (" Hello! Welcome");
printf (" \n On successful program execution, use ‘EXIT_SUCCESS.’ ");
/* Use the "EXIT_SUCCESS" statement instead of using "return 0;" */
EXIT_SUCCESS;
}
Output:

2. void main() function
A "void" keyword refers to an empty data type with no return value. Generally, the "void" data type is used when there is no value to be returned back to the calling function. Furthermore, in the C programming language, it can be used with the main() function to return nothing and may be used with both user-defined and pre-defined functions.
Syntax for void main() function:
void main()
{
/* Statement-1 */
/* Program Body */
}
Example:
Let's consider a program to show how the void main() method works.
#include<stdio.h>
void main()
{
printf(“Hello! This is C programming language!\n”);
printf(“You are in ‘void’ main() function block”);
}
Output:

3. int main(int argc, char ** argv) function:
Command-line parameters can be used to invoke the main() function. It's a function with two parameters of the integer data type (int argc) and the character data type (char * argv). The "argc" and "argv" parameters stand for argument count and argument vector, respectively.
Syntax for the function:
int main(int argc, char *argv[] )
{
/* Statement-1 */
/* Program Body */
}
Here argc means argument count and agrv means argument vector and argument count means the argument vector's length and argument vector means It is a character pointer array.
Example:
Let's look at a command-line argument example where only one argument is passed.
#include <stdio.h>
void main(int argc, char *argv[] )
{
printf(" The Name of the Program is: %s\n", argv[0]);
if(argc < 2)
{
printf("There was no argument given through the command line.\n");
}
else
{
printf("The First Argument is: %s\n", argv[1]);
}
}
The above program can only be compiled and run on the "www.onlinegdb.com" website.
Output:

4. int main(void) function:
An "int main(void)" method (function) returns an integer value, just like the int main() method. The int main() function, on the other hand, allows us to send multiple arguments, whereas the int main(void) function can only be invoked without any arguments.
Syntax for int main(void) function:
int main(void)
{
/* Statement-1 */
/* Program Body */
}
Example:
Consider the following program code to illustrate the int main(void) function in C:
#include <stdio.h>
int main (void) /* It accepts no parameters other than void. */
{
printf (" Hello! Welcome to the World of C language.\n ");
/* It prints both statements */
printf(“You are in the int main(void) function block, which accepts no parameters.”);
return 0;
}
Output:

5. void main(void) function
The "void main (void)" function is identical to the non-returning void main() function in the C programming language. The void main() function, on the other hand, can accept numerous parameters but cannot return a value. It is a null data type, whereas "void main(void)" cannot take any parameters since it contains a predefined method called "main(void)."
Syntax for void main(void) function:
void main(void)
{
/* Statement-1 */
/* Program Body */
}
Example:
Consider the following program to explain the "void main(void)" function in the C programming language:
#include <stdio.h>
void main (void)
{
printf (" Hello! Welcome to the World of C language.\n");
/* It prints both statements */
printf(“You are in the void main(void) function block, which accepts no parameters and no return statement.”);
}
Output:

Conclusion:
- The main() program in the C programming language is a required function that initiates execution of a program. The instructions written inside the main() function in the C programming language are executed in the order they were entered.
- The main() function can also include calls to other functions in the program. Generally, the operating system (OS) controls the main() function in the C language.
- The main() function in the C language is called at run time by the operating system (OS), never at compile time.
- In C, the main function begins with an open bracket "{" and concludes with a close bracket "}." It could return either a void data type or an int data type.
- Because the main() function in the C programming language is a user-defined function, we may also send parameters to it.
- In the case of an "int" (integer) return type, if the return value from the main() function in C is "00," the program has run successfully; otherwise, an error has occurred.
C Function Termination: main()
A program typically terminates whenever it returns from or reaches the conclusion of main, but it can also stop at some other positions in the program for a variety of reasons. For instance, you may need to force the program's termination when an error situation is identified. You can do so by using the "exit" function.