Difference between "int main()" and "int main(void)" in C/C++?
Last Updated :
19 Oct, 2023
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).
Consider the following two definitions of main().
Definition 1:
C
int main()
{
/* */
return 0;
}
C++
int main()
{
/* */
return 0;
}
Definition 2:
C
int main(void)
{
/* */
return 0;
}
C++
int main(void)
{
/* */
return 0;
}
What is the difference?
In C++, there is no difference, both are the same. Both definitions work in C also, but the second definition with the void is considered technically better as it clearly specifies that the 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 the following two C programs (remember to save your files as .c). Note the difference between the two signatures of fun().
Example 1
C
// Program 1 (Compiles and runs fine in C, but not in C++)
void fun() {}
int main(void)
{
fun(10, "GfG", "GQ");
return 0;
}
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
Output of C code
( no output )
Output of C++ code
./Solution.cpp: In function 'int main()':
./Solution.cpp:9:24: error: too many arguments to function 'void fun()'
fun(10, "GfG", "GQ");
^
./Solution.cpp:5:6: note: declared here
void fun() {}
The above program compiles and runs fine in C, but the following program fails in compilation.
Example 2
C
// Program 2 (Fails in compilation in both C and C++)
void fun(void) {}
int main(void)
{
fun(10, "GfG", "GQ");
return 0;
}
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
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 the last example, this example fails in compilation in both C and C++.
From the above, we can conclude that,
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
Q1. Predict the output of the following C programs.
Answer:
C
#include <stdio.h>
int main()
{
static int i = 5;
if (--i) {
printf("%d", i);
main(10);
}
}
Q2. Predict the output of the following C programs.
Answer:
C
#include <stdio.h>
int main(void)
{
static int i = 5;
if (--i) {
printf("%d", i);
main(10);
}
}
Similar Reads
Difference between int *a and int **a in C In C, the declarations int *a and int **a represent two different concepts related to pointers. Pointers play a fundamental role in memory management and data manipulation in C programming so it is important to have a clear understanding of them. What does int * means? This declares a pointer to an
3 min read
Difference between int* p() and int (*p)()? A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, a pointer must be declare before storing any variable address. The general form of a pointer variable declaration is: Syntax: type *var_name; Here, type
2 min read
Difference between Keyword and Identifier in C In C, keywords and identifiers are basically the fundamental parts of the language used. Identifiers are the names that can be given to a variable, function or other entity while keywords are the reserved words that have predefined meaning in the language.The below table illustrates the primary diff
3 min read
Difference between Identifiers and Variables in C Perquisites: Identifiers, Variables Identifiers Identifiers are used for the naming of variables, functions, and arrays. It is a string of alphanumeric characters that begins with an alphabet, or an underscore( _ ) that are used for variables, functions, arrays, structures, unions, and so on. It is
2 min read
Difference between scanf() and gets() in C scanf()It is used to read the input(character, string, numeric data) from the standard input(keyboard).It is used to read the input until it encounters a whitespace, newline or End Of File(EOF). For example see the following code:Â C // C program to see how scanf() // stops reading input after white
3 min read
Difference between #include and #include" " in C/C++ with Examples Pre-requisites: Header files in C/ C++ and its uses The difference between the two types is in the location where the preprocessor searches for the file to be included in the code. #include <filename> // Standard library header #include "filename" // User defined header #include<filename
4 min read
Difference between tellg and tellp in C++ In this article, we will discuss the functionality of basic_istream<>::tellg and basic_ostream<>::tellp and the difference between them. tellg(): The function is defined in the istream class, and used with input streams. It returns the position of the current character in the input strea
2 min read
Difference between C and C++ C++ is often viewed as a superset of C. C++ is also known as a "C with class" This was very nearly true when C++ was originally created, but the two languages have evolved over time with C picking up a number of features that either weren't found in the contemporary version of C++ or still haven't m
3 min read
Difference between user defined function and library function in C/C++ Library Functions These functions are the built-in functions i.e., they are predefined in the library of the C. These are used to perform the most common operations like calculations, updation, etc. Some of the library functions are printf, scanf, sqrt, etc. To use these functions in the program the
3 min read
Difference between declaration of user defined function inside main() and outside of main() The declaration of a user-defined function inside the main() function and outside main() is similar to the declaration of local and global variables, i.e. When we declare a function inside the main, it is in local scope to main() and that function can be used in main() and any function outside main(
4 min read