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:
- It cannot be used anywhere in the program
- in particular, it cannot be called recursively
- its address cannot be taken
- It cannot be predefined and cannot be overloaded: effectively, the name main in the global namespace is reserved for functions
- It cannot be defined as deleted or declared with C language linkage<font color=green (since C++17) ,
inline,static, orconstexpr - 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;
- 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::exitwith the same argument as the argument of the return. (std::exitthen destroys static objects and terminates the program) - 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 impliedstd::exit) are not caught by it.
Main function的更多相关文章
- [Cycle.js] Customizing effects from the main function
How can we show one string on the DOM, and a completely different string on Console log? This lesson ...
- [Cycle.js] Main function and effects functions
We need to give structure to our application with logic and effects. This lessons shows how we can o ...
- The App Life Cycle & The Main Function
The App Life Cycle Apps are a sophisticated interplay between your custom code and the system framew ...
- SetApartmentState(ApartmentState state).Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process
System.Threading.ThreadStateException: 'Current thread must be set to single thread apartment (STA) ...
- Parse the main function arguments
int main(int argc, char **argv) { std::string reportDir; std::string transURL; std::string visualEle ...
- Insert Function before and after main function
Source code: 1: #include<stdio.h> 2: void myStartupFun (void) __attribute__ ((constructor)); 3 ...
- Why java main function is declared as static type?
一个暂且说的过去的解释 The method is static because otherwise there would be ambiguity: which constructor shoul ...
- Adding New Functions to MySQL(User-Defined Function Interface UDF、Native Function)
catalog . How to Add New Functions to MySQL . Features of the User-Defined Function Interface . User ...
- Linux Programe/Dynamic Shared Library Entry/Exit Point && Glibc Entry Point/Function
目录 . 引言 . C/C++运行库 . 静态Glibc && 可执行文件 入口/终止函数 . 动态Glibc && 可执行文件 入口/终止函数 . 静态Glibc & ...
随机推荐
- nyoj 228 士兵杀敌(五)
题目: http://acm.nyist.net/JudgeOnline/problem.php?pid=228 由于该题一开始是进行士兵军功增加,最后才是查找士兵的军功总和,使用一个数组,进行延迟更 ...
- 普通IT和文艺IT工程师的区别
在一个UITableView的editing设置的方法实现过程中,我想到两种写法,顺便想了一下两种方法的区别.觉得这时一个普通IT工程师和NB工程师的区别一个有趣的印记. 您通常时怎么去实现的呢? - ...
- winform文件迁移工具
服务器D盘上传的文件过多,空间剩下很少了,于是想把里面部分文件,大概几万个文件转移到E盘,做了这个小工具.先查询出要转移的文件清单,保存在一个记事本中,如下所示: 接着读取文件名,一个个移动到指定目录 ...
- 【转】adb.exe,start-server' failed -- run manually if necessary
[转]Android adb.exe程序启动不起来,如何处理 解决问题: 百度google大家多说的是任务管理器 kill掉adb 或者重启adb server,但我任务管理器就没有adb ,猜测是某 ...
- Objective-c 数组对象
首先我们必须知道数组的概念:数组是有序的对象集合,一般情况下,一个数组的对象都是相同类型的.数组当中也存在可变数组和不可变数组. 1. 不可变数组 (NSArray) 可变数组 NSMutable 是 ...
- PHP创建定义数组
$array = array(); $array["key"] = "values"; ?> 在PHP中声明数组的方式主要有两种:1.用arr ...
- Android 学习开发笔记《Service 与 Thread 的区别 》
很多时候,你可能会问,为什么要用 Service,而不用 Thread 呢,因为用 Thread 是很方便的,比起 Service 也方便多了,下面我详细的来解释一下. 1). Thread:Thre ...
- java 读文件 解析
[Java]读取文件方法大全 1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile { /** ...
- Ubuntu安装配置TFTP服务
tftpd-hpa 是一个功能增强的TFTP服务器.它提供了很多TFTP的增强功能,它已经被移植到大多数的现代UNIX系统. 1.安装 sudo apt-get install tftpd-hpa t ...
- 为Firefox 添加自定义搜索引擎
网上流传的 about:config[对于新版已经失效] 以及到Firefox安装目录中修改 的方式不知道为什么我没有成功 现在来个简单点得! 首先我们需要一个可以自定义搜索引擎的插件 Organiz ...