What Is The Main In C

Importance points of the main function

  • An operating system always calls the main function when a programmers or users execute their programming code.
  • It is responsible for starting and ends of the program.
  • It is a universally accepted keyword in programming language and cannot change its meaning and name.
  • A main function is a user-defined function in C that means we can pass parameters to the main function according to the requirement of a program.
  • A main function is used to invoke the programming code at the run time, not at the compile time of a program.
  • A main function is followed by opening and closing parenthesis brackets.
  • Syntax

    Example
    
    main()
    {
    // codes start from here
    }
    

    Program to print a statement using main function

Let's explore a program that displays a statement in C without relying on the void and int main functions.

Program.c

Example

#include <stdio.h>
main()
{
printf ("Welcome to the Logic Practice");
}

Output:

Output

Welcome to the Logic Practice

Program to call nested function using main function

Let's explore a scenario where a program invokes a nested function within the main function.

Main.c

Example

#include <stdio.h>
#include <conio.h>	
main()
{
printf (" It is a main() function ");
int fun2(); // jump to void fun1() function
printf ("\n Finally exit from the main() function. ");
}

void fun1()
{
printf (" It is a second function. ");
printf (" Exit from the void fun1() function. ");
}

int fun2()
{ 
void fun1(); // jump to the int fun1() function
printf (" It is a third function. ");
printf (" Exit from the int fun2() function. ");
return 0;
}

Output:

Output

It is a main() function
 Finally exit from the main() function.

Types of the main function

Following are the types of the main function used in C

  • void main
  • int main
  • int main ( int argc, char **argv)
  • int main( void)
  • void main (void)
  • void main function

A void is a reserved word that points to a vacant data type with no output value. Essentially, the void data type is employed when there is no intention to provide a return value to the invoking function. Additionally, it is utilized with the main function to signify no return and is compatible with both user-defined and built-in functions in C programming.

Syntax

Example

void main()
{
// body of the code
}

Program to demonstrate the void main function

Let's create a basic program to showcase the void main function.

void.c

Example

#include <stdio.h>
#include <conio.h>
void main()
{
printf (" C is a programming language. ");
printf( " \n It is a void main() function in C. " );
}

Output:

Output

C is a programming language.
 It is a void main() function in C.

int main function

An int is a reserved word that points to a data type representing whole numbers. When an int data type is paired with the main function, it signifies that the function is expected to provide an integer output. In the case of an int main function, it is necessary to include the return 0; statement towards the conclusion of the main function. This statement indicates the successful completion of the program, while any alternative statement denotes an unsuccessful termination of the program.

Syntax

Example

int main()
{
// code to be executed
return 0;
}

Program to return a value using the int main function in C

Let's create a program that retrieves a value by utilizing an int main function in the C programming language.

prog.c

Example

#include <stdio.h>
int main() 
{
printf (" Welcome to the LOGIC PRACTICE ");
printf( " \n It is an int main() function to return a value. " );
return 0;
}

Output:

Output

Welcome to the LOGIC PRACTICE
 It is an int main() function to return a value.

Program to print the iterative numbers using int main function

Let's explore an illustration showcasing the iterative progression of numbers in C programming through the int main function.

Program.c

Example

#include <stdio.h>
int main()
{
	static int num = 20;
	if ( --num)
		{
			printf (" %d ", num); // print the number
			main(20); 
		}
return 0;
}

Output:

Output

19  18  17  16  15  14  13  12  11  10  9  8  7  6  5  4  3  2  1

We also have the option to utilize an EXITSUCCESS statement, serving as an alternative to the return 0 statement. In cases where the program encounters errors, we can employ EXITFAILURE as a substitute for the missing return 1 statement. It's important to note that the return statement is defined in the standard input-output header file (stdio.h), while the EXIT statement is specified in the standard library header file (stdlib.h).

Program to use the EXIT_SUCCESS statement in int main function

Let's create a C program that utilizes the EXIT_SUCCESS macro instead of the return statement.

Exit.c

Example

#include <stdio.h>
#include <stdlib.h>
int main()
{
	printf (" Hello, Welcome to the World ");
	printf (" \n Use EXIT_SUCCESS on successful execution of the program ");
	EXIT_SUCCESS; // use EXIT_SUCCESS statement in replacement of the return 0;
}

Output:

Output

Hello, Welcome to the World
 Use EXIT_SUCCESS on successful execution of the program

Note: A return statement cannot be used with the void main function because it return a value. Therefore, we can't use it with void main function. However, we can use the EXIT statement with the void main function.

int main (int argc, char *argv)

A main function can be invoked with command line arguments. This function includes two parameters: an integer (int argc) and a character (char *argv) data type. The argc parameter represents the count of arguments, while argv represents the values of the arguments.

int main(void) function

An int main(void) function resembles the int main function in its purpose of returning an integer value. However, while multiple arguments can be passed to the int main, the int main(void) can only be invoked without any arguments.

Program to use the int main(void) function in C

Let's explore a sample program showcasing the int main(void) function in the C programming language.

prog_main.c

Example

#include <stdio.h>
int main (void) // It does not accept any parameter other than void
{
printf (" Welcome to the LOGIC PRACTICE "); // print the statement
return 0;
}

Output:

Output

Welcome to the LOGIC PRACTICE

void main (void) function

A void main (void) function is comparable to the void main function in that it doesn't provide a return value. Nonetheless, the void main function has the capability to receive multiple parameters, although it still doesn't return any value. It represents a null data type, while the void main(void) doesn't require any parameters due to its predefined main(void) function.

Program to use the void main(void) function in C

Let's explore a code example showcasing the void main(void) function in the C programming language.

Main_prog.c

Example

#include <stdio.h>
void main (void) 
{
printf (" Welcome to the LOGIC PRACTICE "); // print the statement
}

Output:

Output

Welcome to the LOGIC PRACTICE

Input Required

This code uses input(). Please provide values below: