In many examples of code on the site there are both records of the form void main() , and int main() for the main function of the program.
Which one is correct and what is the difference?
In many examples of code on the site there are both records of the form void main() , and int main() for the main function of the program.
Which one is correct and what is the difference?
The language standard requires that there are two possible implementations:
int main() int main(int, char**) That is, in both cases, the return type is int . However, the existence of additional options in a particular compiler is allowed:
It is a type of implementation of the type.
Thus, the variant returning int is portable between different implementations of compilers, and void does not apply to those. But in particular, void main() supported by the Microsoft Visual C ++ compiler .
The return value of main generally indicates the success of the program. If everything went well, then zero is returned - return 0; ; otherwise, you should return some other integer value that can be processed on the side that called the program. It is allowed to not explicitly write return 0; in main . In this case, when exiting the function, 0 will be implied. Similar behavior applies to the void version of MS. But if in the case of int the return code can be returned explicitly through return , then with void main() this option will not work :
error C2562: 'main': 'void' function returning a value
However, instead, you can use the function exit(0) , which interrupts the execution of the program and returns the transferred code to the caller.
int main(int argc, char *argv[], char *envp[]); envp - array of environment variables - goldstar_labsThis problem is a linker problem, not a compiler.
You need to understand that main is a _cdecl function, i.e. functions vseravno how many parameters, because, the reference library will have something like this
push ecx; argv push eax; argc call _main pop ecx pop ecx push eax ; 袣芯写 胁芯蟹褉邪褌邪 call _exit ; 袠 薪邪 胁褘褏芯写 褋 泻芯写芯屑 胁芯蟹胁褉邪褌邪 Those. in fact, if the function is int main() , the stack will not spoil, just the function will not see argv and argc. And the void main(int argc, int argv) function void main(int argc, int argv) will get both parameters.
Since the reliance goes on the _main signature, then this signature can be used to put any function that either has no parameters or has the _cdecl tag (if this does not prevent the compiler). To obtain such a signature, you need to specify that you use old signatures, this is done by specifying extern "C" (this indication is present in standard libraries, thanks to which the main works). Those. can roll views of extern "C" int _cdecl main(...); int _cdecl main(...){} extern "C" int _cdecl main(...); int _cdecl main(...){} as well as void main() , int main(int argc) and even long _cdecl main(...); , int _cdecl main(int argc, char** argv, int secparam1, int secparam2, int secparam3)
Now about the return type. The reference library expects a result in eax. When using void , the program can return the garbage returned from eax from this point of view int main() better. But lately, the return code of the program is not analyzed anywhere, so if the f-tion has returned the "garbage" it will most likely not affect anything.
If you write under a batch file, or under a software package (installer for example), where the return code is checked, then you need to write int main() and return the required values.
Also, there are exit terminate functions, and there are exception handlers that interrupt the program, bypassing main - if the program is always interrupted "forcibly" - then the returned type stops playing its role.
Source: https://ru.stackoverflow.com/questions/794522/
All Articles