Is main() a User-Defined Function? [duplicate]
The programmer does define what happens inside main()
, after all.
So, should it be considered a user-defined function?
The C++ standard doesn't have the notion of user-defined functions. Instead, it has the notion of library functions. main is not a library function. However, the standard also imposes some requirements on its signature, and that it must not be overloaded or declared static or inline. In particular, it must not be used, meaning that you cannot call it.
Edit: I originally checked the C standard only. I have now checked the C++ standard as well, and it uses "user-defined" in the following contexts: operators, types, conversions, and libraries. It also has user-declared namespaces, user-declared (default and copy) constructors, and user-written default constructors. It does have "user functions" in 27.1.1.
Yes- main is a user defined function. The easiest way to think of it would be user-defined, but Standard-declared.
It also has other restrictions, for example, non-recursive. However, on some compilers like MSVC, it's allowed to recurse in main(). I find this rather handy.
If it's not a user defined function, what would it be? Clearly not a kernel or library function? Not sure I understand what you are getting at here...
main
function is neither a built-in (predefined) nor user-defined function. It is an exception and you must follow requirements related to it which are stated in C++ standard (e.g. about its presence in program, return type and arguments).
main()
is not a predefined or inbuilt function. It is a user-defined function with a predefined function prototype (also called function signature). The user writes its functionality, but its declaration has certain restrictions.
main()
is neither user-defined nor a built-in library function.
On attempting to compile a C program into an executable, the compiler looks for a function called main
in your list of sources.
Creating a library (either as a shared object or by just giving out the requisite header files) however, has different ramifications.
Just so you know, GCC looks for main
with either one of the following signatures:
int main(int, char **);
OR
int main();
main() is a predefined function from where the code execution starts. If you don't have a main function the program will not run. Hence main is the starting point of the program.
精彩评论