Difference between int main() and int main(void)?

CMainVoid

C Problem Overview


What does the following mean :

int main(void) {...} 

VS

int main() {...}

?

I think that int main() {...} means that main doesn't receive any parameters (from command line) , however:

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

does.

But, what does int main(void) {...} mean? And, what does void stand for ?

I've looked here but it's somehow a different question .

C Solutions


Solution 1 - C

In C++, there is no difference.


In C, the difference is questionable. Some love to argue that the latter version (the one without void) is technically just a common implementation extension and not guaranteed to work by the standard because of the wording in the standard. However, the standard clearly states that in a function definition an empty set of parameters has a well-defined behaviour: that the function does not take any parameters. Thus such a definition for main matches the following description in the standard:

> It [main] shall be defined with a return type of int and with no parameters.

There is, however, a noticeable difference between the two: namely, the version without void fails to provide a correct prototype for the function:

// this is OK.
int main()
{
  if (0) main(42);
}

// this requires a diagnostic to be shown during compiling
int main(void)
{
  if (0) main(42);
}

Oh, and just to be complete: the void has the following meaning in all function declarators:

> (6.7.6.3p10) The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.

Solution 2 - C

First of all, there is a difference of what is allowed for hosted systems and freestanding systems, as shown here.

For hosted systems, 5.1.2.2.1 Program startup applies:

> The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void)

... (more text follows regarding argv/argc etc styles).

The interesting part is "with no parameters". int main() and int main (void) are currently equivalent, since they are both function declarators and have no parameters. The following applies (6.7.6.3 ):

> 10 The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.

>/--/

> 14 An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.145)

Emphasis mine, the bold text is what applies to int main(). There is also note 145) at the end of the text, which says "See ‘‘future language directions’’ (6.11.6)":

>6.11.6 Function declarators > > The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.

And here is the difference. Being a function declarator, int main() is bad style because of the above, since it is not guaranteed to work in the next version of the C standard. It is flagged as an obsolescent feature in C11.

You should therefore always use int main (void) on a hosted system and never int main(), even if the two forms are, for now, equivalent.


In C++ both forms are completely equivalent, but there int main() is the preferred style for subjective, cosmetic reasons (Bjarne Stroustrup says so... which is probably quite a bad rationale for explaining why you do something in a particular way).

Solution 3 - C

In C, in a prototype (not in C++ though) an empty argument list means that the function could take any arguments (in the definition of a function, it means no arguments). In C++, an empty parameter list means no arguments. In C, to get no arguments, you have to use void. See this question for a better explanation.

Solution 4 - C

In C++ having a function foo(void) and foo() is the same thing. However, in C it's different: foo(void) is a function that has no arguments, while foo() is a function with unspecified arguments.

Solution 5 - C

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).

Solution 6 - C

In C++, there is no difference between the two, and int main() is a legal signature and return type for main.

Solution 7 - C

I know the thread is old but this question was bothering me for a while a few years ago so I wanted to throw in my half a cent(if that).

I always treat C functions as if they have fixed amount of arguments regardless of context, unless they use va_args. That is, I trust main to ALWAYS have the prototype:

int main(int argc, char **argv).

even if no arguments are passed, the function has these arguments on the stack because the main function does not have function overloading.

C does have the ability to have primitive overloading through just pretending the argument is not there. In which case, the argument is still passed and is on the stack but you never access it, so it merely reduces size of the source code.

Saying int main() simply means that I know that the function may have parameters, but I am not using them, so I write int main().

Saying int main(void) says that main CERTAINLY has no arguments, and implies that there are two different function prototypes:

int main(void);
int main(int argc, char **argv);

Since C has no function overloading, this is somewhat misleading to me, and I distrust code that has main(void) in it. I would not if main NEVER took any parameters, in which case main(void) would be completely OK.

NOTE: In some implementations, there are more parameters in main than argc and argv, such as env, but this does not bother me because I know that I do not explicitly say that those are the only two parameters, but those are the minimal parameters and it's okay to have more, but not less. This is in contrast to downright saying int main(void) which yells at me as THIS FUNCTION HAS NO PARAMETERS, which isn't true, since it does, they are just omitted.

Here is my basis code:

/* sample.c - build into sample. */
#include <stdio.h>

int main(void)
{
    int _argc = *((int *)2686800);
    char ***_pargv = (char ***)2686804;
    int i;

    for (i = 1; i < _argc; ++i) {
        printf("%s ", (*_pargv)[i]);
    }

    return 0;
}

./sample I clearly have arguments

The function clearly has arguments passed to it, despite going out of the way to explicitly say that it doesn't by typing void into the function prototype.

As eq- says above: > (6.7.6.3p10) The special case of an unnamed parameter of type void as the > only item in the list specifies that the function has no parameters.

Thus saying that the function has void as an argument but actually having arguments on the stack is a contradiction.

My point is that arguments are still there, so explicitly asserting that main is void of arguments is dishonest. The honest way would be to say int main(), which claims nothing about how many parameters it has, only how many parameters you are care about.

NOTE2: The _argc, _pargv are system dependent, to find your values you must find them out by running this program:

/* findargs.c */
#include <stdio.h>

int main(int argc, char **argv)
{
    printf("address of argc is %u.\n", &argc);
    printf("address of argv is %u.\n", &argv);

    return 0;
}

These values should remain correct for your specific system.

Solution 8 - C

In C++, there is no difference between int main() and int main(void).
But in C they are little bit different.
int main() indicates that the main function can be called with any number of parameters or without any parameter. On the other hand, int main(void) indicates that the main function will be called without any parameter

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

> Output: 4 3 2 1

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

> It will show error. Because, in int main(void) parameter is void but in the program we have taken main(10) (which defines some value, not void)

Solution 9 - C

Technically, if your host is partially POSIX compliant, then you have

int main(); // this legacy is always used by the run time library
int main(int argc); // illegal by compiler
int main(int argc, char** argv); // required by C standards
int main(int argc, char** argv, char** envp); // required by POSIX standard

If you have a Mac, there is also this

int main(int argc, char** argv, char** envp, char** apple); // required by Macintosh standard

Your host will send all the arguments, so a host will always send argc, argv, and envp (and apple if you are using an Apple product), but the programmer could have their main declared as taking void. The implicit function pointer typecast is technically an undefined behavior.

To prevent the typecast undefined behavior, int main() is a neutral form that means it could take any fixed number of arguments using canonical type promotion (int or larger, and double or larger) and int main(int argc, ...) means it could take any variable number of arguments also with canonical type promotion. In other words, the form return_type function_name() is an exception to undefined behavior.

Solution 10 - C

In C++:
ㅤㅤint main() and int main(void) are the same in C++. They both take 0 and only 0 parameters.

In C:
ㅤㅤint main() takes as many arguments as you want. The function just won't use them. intㅤㅤㅤ main(void) makes it so passing an argument will create an error and make it impossible to ㅤㅤpass arguments.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJANView Question on Stackoverflow
Solution 1 - Ceq-View Answer on Stackoverflow
Solution 2 - CLundinView Answer on Stackoverflow
Solution 3 - CLinuxiosView Answer on Stackoverflow
Solution 4 - CSome programmer dudeView Answer on Stackoverflow
Solution 5 - CDila GurungView Answer on Stackoverflow
Solution 6 - CjuanchopanzaView Answer on Stackoverflow
Solution 7 - CDmitryView Answer on Stackoverflow
Solution 8 - CChaitalyView Answer on Stackoverflow
Solution 9 - CAbraham LeView Answer on Stackoverflow
Solution 10 - CMadhav Balakrishnan NairView Answer on Stackoverflow