Skip to content
Related Articles

Related Articles

Difference between “int main()” and “int main(void)” in C/C++?

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 14 Jun, 2022
Improve Article
Save Article
 

[Note: This was true for older versions of C but has been changed in C11 (and newer versions).  In newer versions, foo() is same as foo(void). Refer this -> https://port70.net/~nsz/c/c11/n1570.html#6.11.6]

Consider the following two definitions of main(). 
 

CPP




int main()
{
   /*  */
   return 0;
}

Output

 

and 
 

CPP




int main(void)
{
   /*  */
   return 0;
}

Output

 

What is the difference?
In C++, there is no difference, both are same.
Both definitions work in C also, but the second definition with void is considered technically better as it clearly specifies that main can only be called without any parameter. 
In C, if a function signature doesn’t specify any argument, it means that the function can be called with any number of parameters or without any parameters. For example, try to compile and run following two C programs (remember to save your files as .c). Note the difference between two signatures of fun(). 
 

C++




// Program 1 (Compiles and runs fine in C, but not in C++)
 
#include <iostream>
 
void fun() {  }
 
int main(void)
{
    fun(10, "GfG", "GQ");
   
    return 0;
}
 
// This code is contributed by sarajadhav12052009

C




// Program 1 (Compiles and runs fine in C, but not in C++)
 
void fun() {  }
 
int main(void)
{
    fun(10, "GfG", "GQ");
   
    return 0;
}

Output of C code:

 

Output of C++ code:

prog.cpp: In function ‘int main()’:
prog.cpp:10:24: error: too many arguments to function ‘void fun()’
     fun(10, "GfG", "GQ");
                        ^
prog.cpp:6:6: note: declared here
 void fun() {  } 
      ^

The above program compiles and runs fine (See this), but the following program fails in compilation (see this)
 

C++




// Program 2 (Fails in compilation in both C and C++)
 
void fun(void) {  }
 
int main(void)
{
    fun(10, "GfG","GQ");
   
    return 0;
}
 
// This code is contributed by sarajadhav12052009

C




// Program 2 (Fails in compilation in both C and C++)
void fun(void) {  }
int main(void)
{
    fun(10, "GfG", "GQ");
   
    return 0;
}

Output of C/C++ Code:

prog.cpp: In function ‘int main()’:
prog.cpp:8:23: error: too many arguments to function ‘void fun()’
     fun(10, "GfG","GQ");
                       ^
prog.cpp:4:6: note: declared here
 void fun(void) {  }
      ^

Unlike C, in C++, both of the above programs fails in compilation. In C++, both fun() and fun(void) are same.
So the difference is, in C, int main() can be called with any number of arguments, but int main(void) can only be called without any argument. Although it doesn’t make any difference most of the times, using “int main(void)” is a recommended practice in C.
Exercise: 
Predict the output of following C programs.
Question 1 
 

C




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

Question 2 
 

C




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

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!