×

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:

What is the main in C

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:

What is the main in C

Types of the "main()" function in the C programming language

  1. int main() function
  2. void main() function
  3. int main(int argc , char ** argv)
  4. void main(void)
  5. 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:

What is the main in C

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:

What is the main in C

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:

What is the main in C

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:

What is the main in C

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:

What is the main in C

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:

What is the main in C

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:

What is the main in C

Conclusion:

  1. 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.
  2. 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.
  3. The main() function in the C language is called at run time by the operating system (OS), never at compile time.
  4. 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.
  5. Because the main() function in the C programming language is a user-defined function, we may also send parameters to it.
  6. 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.


Related Topics

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

Garbage in C

Garbage in C The C programming language is a perfect fit for embedded systems as it provides low level control, structured programming, and portability. On the contrary, it does not provide...

3 minutes read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Local Labels in C

Anyone who has written programs in the C programming language is required to be familiar with the "go to" and "labels" used in C to navigate between functions. "Local labels"...

4 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

4 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.