Chapter 1.main函数

       下面让我们一起进入C++的世界!
       首先先花点时间复习下C++语言基础部分,然后进入: 标准IO库、标准库(容器、算法、迭代器)、类和数据抽象、面向对象编程、模板与泛型编程、异常处理、命名空间、多重继承与虚继承、优化内存分配、运行时类型识别。
       此系列文章只是作为笔记mark一下,不喜勿喷。
 
//ISO IEC 14882-2011.pdf
Information technology — Programming languages — C++
Third edition
2011-09-01
C++11标准关于Main function [basic.start.main] 叙述如下:
1.A program shall contain a global function called main, which is the designatedstart of the program. It is implementation-defined whether a program in afreestanding environment is required to define a main function. [Note: In afreestanding environment, start-up and termination is implementation-defined; start-upcontains the execution of constructors for objects of namespace scope withstatic storage duration; termination contains the execution of destructors forobjects with static storage duration. —end note ]
2.An implementation shall not predefine the main function. This function shall notbe overloaded. It shall have a return type of type int, but otherwise its typeis implementation-defined. All implementations shall allow both of the following definitions of main:
int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }
In the latter form argc shall be the number of arguments passed to the programfrom the environment in which the program is run. If argc is nonzero thesearguments shall be supplied in argv[0] through argv[argc-1] as pointers to theinitial characters of null-terminated multibyte strings (ntmbss) (17.5.2.1.4.2)and argv[0] shall be the pointer to the initial character of a ntmbs thatrepresents the name used to invoke the program or "". The value ofargc shall be non-negative. The value of argv[argc] shall be 0. [Note: It is recommended that any further (optional) parameters be added after argv.—end note ]
3.The function main shall not be used within a program. The linkage (3.5) of mainis implementation-defined. A program that defines main as deleted or thatdeclares main to be inline, static, or constexpr is ill-formed. The name mainis not otherwise reserved. [Example: member functions, classes, andenumerations can be called main, as can entities in other namespaces. —end example ]
4.Terminating the program without leaving the current block (e.g., by calling thefunction std::exit(int)(18.5)) does not destroy any objects with automaticstorage duration (12.4). If std::exit is called to end a program during thedestruction of an object with static or thread storage duration, the programhas undefined behavior.
5.A return statement in main has the effect of leaving the main function (destroying any objects with automaticstorage duration) and calling std::exit with the return value as the argument.If control reaches the end of main without encountering a return statement, the effect is that of executing
return 0;
 
        第2条可知main() 函数可有两种形式
int main() { /* ... */ }

int main(int argc, char* argv[]) { /* ... */ }
        其中 It is recommended that any further (optional) parameters be added after argv. 这个是平台有关的第三个参数。
        如在windows操作系统下:
int main(int argc, char *argv[], char*env[])
{
      cout << argv[0]<< '\t' << env[0] << endl;
}
        env[0]打印出来的是cmd窗口下set 命令返回的第一个环境变量值。
 

        如在linux操作系统下:

int main(int argc, char *argv[], char*env[])
{
	cout << argv[0]  << '\t' << env[0] <<endl;
	char*home, *hostname;
	home = getenv("HOME");
	host = getenv("HOSTNAME");
	cout << home << '\t' << hostname << endl;
}

        类似的函数有putenv、setenv,、unsetenv等,第三个参数提供了轻量级的环境变量访问等。
 
        第5条主要就是说如果到main()函数结束都没有遇到一个返回语句,便相当于执行了一句return 0; 语句一样的作用。所以可以不必写 return 0; 来结尾。在大多数系统中返回值 0 表示main函数成功执行,所以个人习惯是追加一个
return 0;
来表示main函数成功执行完毕。
 
        查看main()函数的返回值
        在UNIX系统中是
echo $?
        在windows系统中是
echo %errorlevel%

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值