C++系列文章 main函数(2007-04-24 15:33:04)

标签: c++ microsoft returning function pointers parameters
803人阅读 评论(1) 收藏 举报

在开始学C++编程时,main函数是一个完整C++程序所必须有的。但在不同的书中,具体格式会有所不同,在nell dale的Programming In c++中是int main() ,在有些书中及C++ Builder6生成的模版中是int main(int argc, char* argv[]),这是为什么呢?读一下下面的短文就知道了。(以下摘自ISO/IEC14882,也即C++ 1998标准 )

3.6.1 Main function [basic.start.main]
1 A program shall contain a global function called main, which is the designated start of the program. It is implementationdefined whether a program in a freestanding environment is required to define a main function. [Note: in a freestanding environment, startup and termination is implementationdefined; startup contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. ]

2 An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementationdefined. All implementations shall allow both of the following definitions of main:
int main() { }
int main(int argc, char* argv[]) { }

In the latter form argc shall be the number of arguments passed to the program from the environment in which the program is run. If argc is nonzero these arguments shall be supplied in argv[0] through
as pointers to the initial characters of nullterminated
multibyte strings (NTMBSs)
( and argv[0] shall be the pointer to the initial character of a NTMBS that represents the name used to invoke the program or "". The value of argc shall be nonnegative. The value of argv[argc] shall be 0. [Note: it is recommended that any further (optional) parameters be added after argv. ]

3 The function main shall not be used (3.2) within a program. The linkage (3.5) of main is implementationdefined.
A program that declares main to be inline or static is illformed.
The name main is not otherwise reserved. [Example: member functions, classes, and enumerations can be called main, as can entities in other namespaces. ]

4 Calling the function
void exit(int);
declared in (18.3) terminates the program without leaving the current block and hence without destroying any objects with automatic storage duration (12.4). If exit is called to end a program during the destruction of an object with static storage duration, the program has undefined behavior.

5 A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling 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;




main returning void is Microsoft Specific

This statement is Microsoft Specific but is outside the the Microsoft Specific section markers:

Alternately, the main and wmain functions can be declared as returning void (no return value). If you declare main or wmain as returning void, you cannot return an exit code to the parent process or operating system using a return statement; to return an exit code when main or wmain is declared as void, you must use the exit function.

ANSI C or C++ requires that main() return an int of 0 or 1 only.

Please note that Visual Studio.NET 2005 reports an error when main() returns void. You have to enable Microsoft Extensions for the error to go away.


go时间格式化必须精确地限定到 golang 指定的时间原点:2006-01-02 15:04:05

结论:年、月、日、时、分、秒,英文、数字,必须精确地限定到 golang 指定的时间原点:2006-01-02 15:04:05...
  • keyunq
  • keyunq
  • 2017年12月15日 11:33
  • 208

resourcemanager ha 配置

http://blog.csdn.net/u011414200/article/details/50336735 http://han-zw.iteye.com/blog/2324114 ...
  • lepton126
  • lepton126
  • 2017年06月24日 15:35
  • 336

ROS学习(基于Ubuntu 15.04 和ROS Jade)第一章 安装ROS到Ubuntu主机

前言本系列作为ROS笔记,所以内容来自http://wiki.ros.org网站。  14年工作时接触到FANUC工业机器人,从外观上来看,它应该准确的称作机器人类别里面的机械手臂(arm),它美中...
  • jrc_january
  • jrc_january
  • 2017年04月08日 16:54
  • 223

Golang 奇葩的时间包基本用法(一)

golang 时间包的常见用法
  • zhengzizhi
  • zhengzizhi
  • 2017年06月21日 00:50
  • 147

Centos 查看系统版本

[root@bogon ~]# uname -a Linux bogon 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x...
  • kongshuai19900505
  • kongshuai19900505
  • 2017年10月31日 14:09
  • 490


  • duan19920101
  • duan19920101
  • 2016年02月22日 14:19
  • 501

linux c/c++程序是否都是从main函数开始?

请抛弃思维定势 以下为《程序员自我修养》片断,共勉
  • timegoesby001
  • timegoesby001
  • 2013年11月28日 19:18
  • 1158

C++ main函数详解

main函数的参数   我们经常用的main函数都是不带参数的。因此main 后的括号都是空括号。实际上,main函数可以带参数,这个参数可以认为是 main函数的形式参数。C语言规定mai...
  • hgl868
  • hgl868
  • 2011年12月09日 16:55
  • 6155


我们看下面的例子:          /* file name test00.c */          int main(int argc, char* argv)     {      retur...
  • xiongbixb2
  • xiongbixb2
  • 2015年11月08日 10:40
  • 830


以前编程的时候从未注意到这个小细节,今天用Codeblocks运行时出现错误,好奇查了一下,才发现一直以来不曾注意的一个小知识点。代码如下:  #include using namespace s...
  • onlymytravel
  • onlymytravel
  • 2017年03月06日 17:24
  • 318
    访问量: 19万+
    积分: 3275
    排名: 1万+