Home » What is the main in C

What is the main in C

by Online Tutorials Library

What is the main in C?

In this topic, we will discuss the main in C programming language. A main is a predefined keyword or function in C. It is the first function of every C program that is responsible for starting the execution and termination of the program. It is a special function that always starts executing code from the ‘main‘ having ‘int‘ or ‘void‘ as return data type. In other words, a main() function is an entry point of the programming code to start its execution.

What is the main in C

Importance points of the main() function

  1. An operating system always calls the main() function when a programmers or users execute their programming code.
  2. It is responsible for starting and ends of the program.
  3. It is a universally accepted keyword in programming language and cannot change its meaning and name.
  4. 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.
  5. A main() function is used to invoke the programming code at the run time, not at the compile time of a program.
  6. A main() function is followed by opening and closing parenthesis brackets.

Syntax

Program to print a statement using main() function

Let’s consider a program to print a statement without using the void and int main() function in C.

Program.c

Output:

Welcome to the tutoraspire  

Program to call nested function using main() function

Let’s consider a program to call nested function inside the main() function.

Main.c

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

  1. void main()
  2. int main()
  3. int main ( int argc, char **argv)
  4. int main( void)
  5. void main (void)

void main() function

A void is a keyword that references an empty data type that has no return value. In other words, the void data type is used when we don’t want to return any value to the calling function. Furthermore, it is used with the main() function to return nothing and can be used with user-defined and predefined functions in C programming.

Syntax

Program to demonstrate the void main() function

Let’s write a simple program to demonstrate the void main() function.

void.c

Output:

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

int main() function

An int is a keyword that references an integer data type. An int data type used with the main() function that indicates the function should return an integer value. When we use an int main() function, it is compulsory to write return 0; statement at the end of the main() function. The return 0; statement represents that the program has been successfully executed, whereas any other statement represents the unsuccessful termination of the program.

Syntax

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

Let’s write a program to return a value using an int main() function in C language.

prog.c

Output:

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

Program to print the iterative numbers using int main() function

Let’s consider an example to display the iterative sequence of number in C using int main() function.

Program.c

Output:

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

We can also use an EXIT_SUCCESS statement, which is the alternative statement of the return 0. If the program is not successfully executed, we can use the EXIT_FAILURE in the absence of the return 1 statement. The definition of the return statement in the standard input-output header file (stdio.h), whereas the EXIT statement is defined in the standard library (stdlib.h) header file.

Program to use the EXIT_SUCCESS statement in int main() function

Let’s write a program in C to use the EXIT_SUCCESS statement in replacement of the return statement.

Exit.c

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 called using command line arguments. It is a function that contains two parameters, integer (int argc) and character (char *argv) data type. The argc parameter stands for argument count, and argv stands for argument values.

int main(void) function

An int main(void) function is similar to the int main() function to return an integer value. But we can pass more than one argument to the int main(), whereas the int main(void) can only be called without any argument.

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

Let’s consider a program to demonstrate the int main(void) function in C language.

prog_main.c

Output:

Welcome to the tutoraspire  

void main (void) function

A void main (void) function is similar to the void main() function that does not return a value. However, the void main() function can accept multiple parameters, but it does not return a value. It is an empty data type, whereas the void main(void) does not take any parameter because it has a predefined main(void) function.

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

Let’s consider a program to demonstrate the void main(void) function in C language.

Main_prog.c

Output:

Welcome to the tutoraspire  

You may also like