Difference Between void main(), int main() and void main(void) in C/C++

The main function is the one where the program initiates its execution. All the other functions are called from the main function. But because of the feature “overloading” present in C programming, the main function can be called using different return types or parameters. We will discuss those types of main in this tutorial. While working with C programming language you may crossways with different types of main functions and these types depend majorly on the number of parameters and the return type of the main functions. In this article, we are going to discuss the following types of the main function.

  • void main ()
  • void main (void)
  • Int main (int)

Void main()

In this function void represents the return type of the main function. However, as we know void means “nothing”, such main functions return nothing and the number of items inside the parenthesis represents the number of arguments a main function will take. If the parenthesis are empty this means that the main function can take multiple arguments. This feature of main function is only restricted to C language. For  C++ sending an arguments to such a function during function call will generate an error. Lets have a simple coding example of this main function.

Example Code

#include <stdio.h>
void main() {
   static int counter = 3;
   if (--counter){
      printf("%d ", counter);
      main(5);
   }
}

Program Output

difference between void main and int main 1

Void main (void)

The return type of this main function is same as the one discuss previously i.e. “void” means nothing. The only difference in both these functions is the arguments. You must be wondering that void is nothing and this function should be similar with the previous function because that function also takes nothing as an argument?

But, this is absolutely wrong. Leaving the arguments portion of empty means we can provide multiple arguments in its call. Giving void as an argument means that the function DO NOT accept any argument. Providing arguments to such function will return an error. This “void” in the parameters of the main function call will restrict the user from multiple arguments which is a good practice. An example code is shown below,

Example Code

#include <stdio.h>
void main(void) {
   static int counter = 3;
   if (--counter){
      printf("%d ", counter);
      //main(5);
   }
}

Program Output

Sending argument to such main function will result in error.

difference between void main and int main 2

int main (void)

This function is partially similar to the above function “void main (void)” as the arguments to these two functions are the same (void). This function also doesn’t accept anything as a parameter during function call and restricts the users from sending multiple parameters in the function call.

Now comes the “int” part of this function. It represents the return type of the main function. The return type of this function is an integer. If the main function exists successfully than this function returns a 0 value in the int and if an error occurs during the execution of the main code then the value depends on the occurred error. The example of such a main function is shown in the code below.

Example Code

#include <stdio.h>
int main(void) {
   static int counter = 3;
   if (--counter){
      printf("%d ", counter);
   }
   return 0;
}

Program Output

difference between void main and int main 3

Related Article

  1. C Program Compilation steps & Example with GCC Linux
  2. Introduction to Pointers
  3. How to find an array size without using sizeof()
  4. Pointer Arithmetic, Pointer Size, and Pointer Type
  5. In which order function parameters are evaluated in C/C++?
  6. Scope of global and static variables in C/C++
  7. Difference between Structure and Union in C
  8. C Program to remove duplicate elements from an array

Leave a Reply