Main() Function Implementation C Program

All About Main Function in C Programming

Get to know about the main() function in C programming language. main() function is probably the most important function in most of the programming languages. Let us see different implementations of main() methods which will help you differentiate between different implementations. You will find here almost all the details about main function in c language here.

What is main() function?

Every C program starts its execution with the main() function. It is a mandatory function to compile and run c programs. It gives your c compiler a starting point where the compiler can start its execution from. main() function can be also regarded as the captain of the ship. It helps to execute the flow of the program.

In other words, main() method is an entry point where the processor enters a computer program (C program) and begins executing it. Most of the programming languages have a single entry point such as main for execution purpose. The main function is, therefore, called at the program startup.

There are different variations of main() function such as:

void main()

int main()

int main(void)

void main(void)

int main(int argc, char **argv)

Let us now see how to declare a function and most important implementations of the main function in C programming.

Function Declaration Syntax

return-type function name(arguments-list);

Example

int main(void)

Note: All the C Programs listed below has been compiled with GNU GCC Compiler and developed using gEdit Editor and Terminal in Linux Ubuntu Operating System.

void main() function

#include<stdio.h>
void main()
{
printf("Hello World");
}

void is the return type here which means that this main() method does not return any value. Empty round brackets () indicates that there are no arguments passed to the function. void main() is the most commonly used implementation of the main() method in C programming.

int main() function

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

In this case, the main method returns a value which is 0 here. Again, there are no arguments passed to the main() function in this program. int main() is widely used implementation of main() function in C++ programming.

If int main() method is used, then it is mandatory to write a return 0; statement at the end of the main() function. The argument of the return statement indicates the compiler if the program has been terminated successfully or it has failed in executing properly.

The return 0 statement indicates the program has executed successfully whereas any value other than zero indicates unsuccessful termination of the program. return 1 or even return -1 can be written for unsuccessful termination.

An alternative statement for return 0 statement is EXIT_SUCCESS and the alternative for return 1 is EXIT_FAILURE. However, return statements have its definition in the standard input output header file (stdio.h) whereas EXIT statements have its definition in the standard library (stdlib.h). Let us see an example below.

EXIT_FAILURE

EXIT_SUCCESS

Note: return statement cannot be used with void main() methods. However, EXIT statements can be used with void main() function.

int main(void) and void main(void) simply means that the functions do not take in any parameters or arguments. It is similar to the normal implementation int main() and void main(). void actually represents NULL or Empty.

Command Line Arguments – int main(int argc, char *argv)

The main function can also be invoked through command line arguments. Here, the argument argc represents number of arguments whereas argv represents a vector of the arguments. It is, therefore, possible to pass parameters to the function while it is executing through the command line arguments.

argc: argument count

It consists of Non-negative values that indicates the compiler about the number of arguments to be passed to the main() function.

argv: argument vector

It is a pointer to array element. It points to the elements of the array which consists of the arguments being passed to the main() method.

Command Line Argument Implementation

Output

Important Points

main() function is used to invoke the program code at run time and not at the compilation time.

main() and int main() are one and the same. main() by default, means that it’s return-type is int.

Is main() function user-defined or pre-defined?

You must be confused if main function in c is user defined or not. Well, let us also look into this matter.

As you can see in the above codes, the main function() is defined by the user or the programmer. Therefore, main is definitely a user defined function. The main function is, therefore, a user implemented function. However, the prototype of the main method is pre-defined by the compilers. As mentioned above, the main method has different implementations considering its return types and arguments. Therefore, it’s prototype is already defined by the compiler.

Therefore, main method is a user defined function with a pre-defined prototype.

If you have any compilation errors or doubts about main function in C language, let us know about it in the comment section below.