main() 是用户定义函数吗?
毕竟,程序员确实定义了main()
内部发生的事情。
那么,它应该被视为用户定义的函数吗?
The programmer does define what happens inside main()
, after all.
So, should it be considered a user-defined function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
C++ 标准没有用户定义函数的概念。相反,它具有库函数的概念。 main 不是库函数。但是,该标准还对其签名提出了一些要求,并且不得重载或声明为静态或内联。特别是,它不能被使用,这意味着您不能调用它。
编辑:我最初只检查了 C 标准。我现在也检查了 C++ 标准,它在以下上下文中使用“用户定义”:运算符、类型、转换和库。它还具有用户声明的命名空间、用户声明的(默认和复制)构造函数以及用户编写的默认构造函数。它确实在 27.1.1 中具有“用户功能”。
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.
是的-main是一个用户定义的函数。最简单的想法是用户定义,但标准声明。
它还具有其他限制,例如非递归。但是,在某些编译器(例如 MSVC)上,允许在 main() 中递归。我发现这相当方便。
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
函数既不是内置(预定义)函数,也不是用户定义函数。这是一个例外,您必须遵循 C++ 标准中规定的与之相关的要求(例如,关于它在程序中的存在、返回类型和参数)。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()
不是预定义或内置函数。它是一个用户定义的函数,具有预定义的函数原型(也称为函数签名)。用户编写其功能,但其声明有一定的限制。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()
既不是用户定义的函数,也不是内置的库函数。在尝试将 C 程序编译为可执行文件时,编译器会在源列表中查找名为
main
的函数。然而,创建库(作为共享对象或仅提供必要的头文件)具有不同的后果。
如您所知,GCC 会查找具有以下任一签名的
main
:int main(int, char **);
或
int main();
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() 是一个预定义函数,代码从这里开始执行。如果没有 main 函数,程序将无法运行。因此 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.