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

Which means the following:

int main(void) {...} 

VS

 int main() {...} 

?

I think int main() {...} means that main does not receive any parameters (from the command line):

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

does.

But what does int main(void) {...} mean? what does void mean?

I looked here , but this is somehow a different question.

+49
c main void
Sep 01 '12 at 5:30
source share
8 answers

In C ++ there is no difference.




In C, the difference is doubtful. Some people like to say that the latest version (without void ) is technically just a general extension of the implementation and is not guaranteed to work according to the standard due to the wording in the standard. However, the standard clearly states that in the definition of a function an empty set of parameters has a well-defined behavior: the function does not accept any parameters. Thus, such a definition for the core corresponds to the following description in the standard:

It [main] must be defined with an int return type and no parameters.

However, there is a noticeable difference between the two: namely, the version without void cannot provide the correct function prototype:

 // 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: void has the following meaning in all function declarators:

(6.7.6.3p10) A special case of an unnamed parameter of type void as the only element in the list indicates that the function has no parameters.

+46
01 Sep
source share

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

+6
01 Sep '12 at 5:37
source share

In C ++ having the function foo(void) and foo() , this is the same thing. However, in C this is different: foo(void) is a function that has no arguments, and foo() is a function with undefined arguments.

+4
01 Sep
source share

There is no difference in C ++, both are the same.

Both definitions work in C, but the second definition with void is considered technically better, since it clearly indicates that main can only be called without any parameter. In C, if the signature of the function does not indicate any argument, this means that the function can be called with any number of parameters or without any parameters. For example, try compiling and running the following two C programs (remember to save your files as .c).

+3
May 23 '15 at 3:05
source share

First of all, there is a difference in what is allowed for hosted systems and stand-alone systems, as shown here .

For hosted systems, 5.1.2.2.1, the program launch is applied:

The function called when the program starts is called main. The implementation does not declare a prototype for this function. It is defined with an int return type and without Parameters:

 int main(void) 

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

The interesting part is “no parameters”. int main() and int main (void) are currently equivalent, as they are both function declarators and have no parameters. Applicable (6.7.6.3):

10 A special case of an unnamed parameter of type void as the only element in the list indicates that the function has no parameters.

/ - /

14 The identifier list only declares function parameter identifiers. An empty list in a function declaration, which is part of the definition of this function, indicates that the function has no parameters. An empty list in a function declaration that is not part of the definition of this function indicates that no information about the number or types of parameters is supplied .145)

Emphasize the target, bold text is what int main() refers to. There is also note 145) at the end of the text that says “See” “further language directions (6.11.6)”:

6.11.6 Function declaration

Using function declarators with empty brackets (not prototype parameter type declarators) is an obsolete function.

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

Therefore, you should always use int main (void) on the hosted system and never int main() , even if the two forms are currently 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 a pretty poor explanation of why you are doing something in a certain way).

+2
Jul 10 '15 at 8:48
source share

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

+1
Sep 01
source share

The prototype of the Type foo(void) function is exactly the same as Type foo() , there is no difference between them. The former can be used for readability.

As with main - accepting arguments or not, the program can still access command line information using other means, such as __argv , __argc , GetCommandLine or other platform / compiler characters.

0
Sep 01 '12 at 5:33
source share

I know that the stream is old, but a few years ago this question bothered me, so I wanted to give up my half percent (if that).

I always consider C functions as if they fixed the number of arguments regardless of context, unless they use va_args. That is, I believe that ALWAYS have a 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 has no function overload.

C has the ability to have primitive overloads by simply pretending that the argument is missing. In this case, the argument is still passed and is on the stack, but you never access it, so it just reduces the size of the source code.

The statement int main () just means that I know that the function can have parameters, but I do not use them, so I write int main ().

The statement int main (void) means that main SOME has no arguments and implies the presence of two different prototypes of functions:

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

Since C does not have an overload function, this is somewhat misleading, and I do not trust the code with the main (void) in it. I would not if main NEVER accepted any parameters, in which case main (void) would be completely fine.

NOTE. In some implementations, there are more parameters mainly than argc and argv, such as env, but this does not bother me, because I know that I do not say directly that these are only two parameters, but these are minimal parameters, and everything is fine to have more, but not less. This contradicts the direct statement of int main (void), which yells at me, because THIS FUNCTION IS NO PARAMETERS, which is not true, because it is, they are simply omitted.

Here is my base 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 explicitly have arguments

The function obviously has arguments passed to it, despite the fact that it does not explicitly say that it does not type void into the function prototype.

As mentioned above:

(6.7.6.3p10) A special case of an unnamed parameter of type void as soon as an element in the list indicates that the function has no parameters.

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

My point is that the arguments still exist, so the explicit statement that main contains no arguments is dishonest. The honest way would be to say int main (), which does not say anything about how many parameters it has, only about how many parameters you need.

NOTE2: _argc, _pargv depend on the system to find your values ​​that you must find 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 valid for your specific system.

0
Mar 24 '16 at 2:47
source share



All Articles