Various declarations of main() function in C

main() is a special function in C programming language. Reasons that make it special are -

  • It defines starting point of the program.
  • main is the first executed function.
  • It controls all other child functions.
  • Behaves as both user-defined and pre-defined function.
  • Every software written in C must have a main function.

C is a popular programming language. There exist many compilers and standards for C. So exists many variants of main function declaration. In this post we will learn various declarations of main one by one. Later we will see the standard definition of the main function in C.

Various main() function declarations

  1. main()
  2. main(int argc, char * argv[])
  3. main(int argc, char ** argv)
  4. int main()
  5. int main(void)
  6. int main(int argc, char * argv[])
  7. int main(int argc, char ** argv)
  8. int main(int argc, char * argv[], char *envp[])
  9. int main(int argc, char ** argv, char ** envp)
  10. void main()
  11. void main(void)
  12. void main(int argc, char * argv[])
  13. void main(int argc, char ** argv)

main()

Some of the older C compilers supports a feature of default return type of function. It uses default return type int if not mentioned explicitly.

This definition of the main function accepts no parameters and returns an integer. Here we optionally need a return statement to return the integer value.

Note: New compilers may not supports this declaration.

main(int argc, char * argv[]) main(int argc, char ** argv)

This declaration of main is extension of previous. Optionally it accepts command line arguments from the user. It also returns an integer value by default.
Here the first parameter argc is total number of command line arguments passed.
argv is an array of string containing all command line arguments passed to the program.

int main()

This is one of the standard main function declaration used across various compilers. It may or may not accept any parameter and returns an integer value at the end of program. In this entire series of C tutorial I will use this declaration of main.

Unlike upper two definitions, we must use return keyword to return an integer value at the end of program. Returning zero specifies that the program completed all desired operation and terminated successfully. A non-zero value specifies that the program terminated without completing all desired operations.

Note: This definition of main is not qualified standard definition and not used in real life projects. It is superseded by the below definition.

int main(void)

int main(void) is qualified definition of main function. It looks similar to int main(). However, there is little difference between both.

int main() may or may not accept any arguments. Whereas int main(void) will never accept an argument. The void keyword restricts it to accept any arguments.

Important note: Arguments passed to the int main(void) are ignored.

int main(int argc, char * argv[]) int main(int * argc, char ** argv)

The above two declarations convey same meaning. These two declarations are also a part of the current C standard. We use this when we need to pass command line arguments to the main function.

It accepts two parameters argc and argv and returns an integer.

The parameter argc is total number of arguments passed to the main.
Whereas argv is an array of string, containing all command line arguments passed.

int main(int argc, char * argv[], char * envp[]) int main(int argc, char ** argv, char ** envp)

This is less known declaration of main in C. It accepts three parameters and like other standard main definitions, returns an integer.

The parameter argc specifies total command line arguments passed.
The first array of string argv contains all command line arguments passed.
The second array of string envp contains list of all environment variables.

This declaration of main can be handy when we need access to the environment variables locally.

void main()

This is a non-standard way to declare main function. Yet, many beginner C programmers uses this due to its easiness. Modern C compilers may not support this declaration.

It may or may not take any argument and returns void. Returning void to the operating system is considered as poor programming practice. As nobody could ever know whether the program terminated successfully or not.

void main(void)

This declaration of main is similar to the previous definition except in the terms of parameters. As like the previous definition of main, it returns nothing at the end of the program. In addition it ensures that, it does not accepts any parameter.

void main(int argc, char * argv[]) void main(int argc, char ** argv)

This is also a non-standard way to declare main function. You may use this definition to receive command line arguments from user. It returns nothing but takes two parameters argc and argv.

The parameter argc contains the total command lines arguments.
The parameter argv is an array of string. It contains all command line arguments passed.

Standard declaration main() function

We saw various ways of writing main method. However, not all are based on C standard. For small programs or projects, you are free to use any of these declarations if your compiler supports. For large-scale projects, you must follow C standards.

Following a proper standard is often recommended or mandatory for large projects. You may think, why do I need to follow a C standard? Here are some major advantages of following a C standard.

Why should I follow C standard?

  • Following a C standard ensures portability among various compilers.
  • Following a C standard would not lock you down if you want backward compatibility.
  • C standard ensures maximum productivity among the project members.

Now, let us look on to the ANSI C standard of declaring main function. ANSI C has specified two standard declaration of main.

  1. int main(void)
  2. int main(int argc, char * argv[]) or int main(int argc, char ** argv)

However, for small programs you are can use the simple declaration int main(). It is not an ASNI C standard and not recommended for large projects.

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>