zoukankan      html  css  js  c++  java
  • Main function

    Main function

    A program shall contain a global function named main, which is the designated start of the program.

    int main () { body }											(1)	
    int main (int argc, char *argv[]) { body }						(2)	
    int main (int argc, char *argv[] , other_parameters ) { body }	(3)	
    

    argc - Non-negative value representing the number of arguments passed to the program from the environment in which the program is run.

    argv - Pointer to the first element of an array of pointers to null-terminated multibyte strings that represent the arguments passed to the program from the execution environment (argv[0] through argv[argc-1]). The value of argv[argc] is guaranteed to be ​0​.

    body - The body of the main function

    other_parameters - Implementations may allow additional forms of the main function as long as the return type remains int. A very common extension is passing a third argument of type char*[] pointing at an array of pointers to the execution environment variables.

    The names argc and argv are arbitrary, as well as the representation of the types of the parameters: int main(int ac, char** av) is equally valid.

    Explanation

    The main function is called at program startup after initialization of the non-local objects with static storage duration. It is the designated entry point to a program that is executed in hosted environment (that is, with an operating system). The entry points to freestanding programs (boot loaders, OS kernels, etc) are implementation-defined.

    The parameters of the two-parameter form of the main function allow arbitrary multibyte character strings to be passed from the execution environment (these are typically known as command line arguments), the pointers argv[1] .. argv[argc-1] point at the first characters in each of these strings. argv[0] is the pointer to the initial character of a null-terminated multibyte strings that represents the name used to invoke the program itself (or an empty string "" if this is not supported by the execution environment).

    The strings are modifiable, although these modifications do not propagate back to the execution environment: they can be used, for example, with std::strtok. The size of the array pointed to by argv is at least argc+1, and the last element, argv[argc], is guaranteed to be a null pointer.

    The main function has several special properties:

    1. It cannot be used anywhere in the program
    • in particular, it cannot be called recursively
    • its address cannot be taken
    1. It cannot be predefined and cannot be overloaded: effectively, the name main in the global namespace is reserved for functions
    2. It cannot be defined as deleted or declared with C language linkage<font color=green (since C++17) , inline, static, or constexpr
    3. The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;
    4. Execution of the return (or the implicit return upon reaching the end of main) is equivalent to first leaving the function normally (which destroys the objects with automatic storage duration) and then calling std::exit with the same argument as the argument of the return. (std::exit then destroys static objects and terminates the program)
    5. If the main function is defined with a function-try-block, the exceptions thrown by the destructors of static objects (which are destroyed by the implied std::exit) are not caught by it.
  • 相关阅读:
    Python编程 从入门到实践-2变量上
    NX二次开发-基于Winform界面对话框与NXOPEN C#交互的开发(对话框嵌套)
    NX二次开发-UFUN获取投影曲线里的曲线UF_CURVE_ask_proj_curves
    NX二次开发-UFUN获取投影曲线里的曲线UF_MODL_ask_proj_curves
    NX二次开发-UFUN创建投影曲线UF_MODL_create_proj_curves
    NX二次开发-NXOPEN C#项目如何设断点调试代码
    NX二次开发-外部开发模式exe(不打开NX进行后台操作)以及封装exe传参调用
    NX二次开发-工程图模板,标题栏,页码,日期,比例,单位,部件名,等自动更新【转载】
    QTreeWidget 遍历所有子节点(QTreeWidgetItem)【转载】
    NX二次开发-使用NXOPEN C#手工搭建开发环境配置
  • 原文地址:https://www.cnblogs.com/Wojoin/p/5211166.html
Copyright © 2011-2022 走看看