| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 827 人关注过本帖
标题:What's the difference between main() / void main() / int main() / int ...
只看楼主 加入收藏
aaronhexin
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:67
专家分:199
注 册:2010-12-6
结帖率:100%
收藏
 问题点数:0 回复次数:3 
What's the difference between main() / void main() / int main() / int main(void)
A very common question is "What's the difference between void main and int main?". This particular FAQ tries to answer that and more, covering other versions of the main() implementation.

The first thing to note is that this is one of those topics that people seem to like to argue over for hours, days and more. Some arguments are valid, some are not, and some are just plain old opinion.

The C and C++ standards differ when it comes to main(), so I'll detail each one separately.

For C

Under C89, main() is acceptable, although it is advisable to use the C99 standard, under which only these are acceptable:

int main ( void )
int main ( int argc, char *argv[] )

Slight variations of the above are acceptable, where int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.

The first option is used when you do not require access to the command line arguments.

The names argc and argv are identifiers that can be changed if you so desire, but sticking to argc/argv is convention.

The return type of main() must always be an int, this allows a return code to be passed to the invoker.

Under C89, the return statement at the end of main() is required, whereas under C99 if no return statement is present, return 0 is implied. However, it is good programming practice to always use a return statement, even if you don't have to.

For C++

The following are acceptable uses:

int main ( int argc, char *argv[] )
int main ()

The first option follows similar conventions to those used by C99.

The second option is used when you do not require access to the command line arguments, and is equivalent to the int main(void) option used by C99.

Again, the return type must always be an int, and the function should return 0; at the end, but it is not required by the standard.

(C) The difference between int main() and int main(void)

A common misconception for C programmers, is to assume that a function prototyped as follows takes no arguments:

int foo();

In fact, this function is deemed to take an unknown number of arguments. Using the keyword void within the brackets is the correct way to tell the compiler that the function takes NO arguments.

What's the deal with void main()

Under regular function calling/returning in C and C++, if your don't ever want to return anything from a function, you define it's return type as void. For example, a function that takes no arguments, and returns nothing can be prototyped as:

void foo(void);

A common misconception is that the same logic can be applied to main(). Well, it can't, main() is special, you should always follow the standard and define the return type as int. There are some exceptions where void main() is allowed, but these are on specialised systems only. If you're not sure if you're using one of these specialised systems or not, then the answer is simply no, you're not. If you were, you'd know it.

Be warned that if you post your "void main" code on the forums, you're going to get told to correct it. Responding with "my teacher said it's OK" is no defence; teachers have a bad habit of being wrong. Be safe, and post only standard code, and you'll find people concentrate on answering your other problems, rather than waste time telling you about this type of thing.

But what about int main(int argc, char *argv[], char *envp[])

As an extension to the guaranteed standard, an additional parameter to main() can, on some systems, be used to gain access to the environment variables. This is isn't guaranteed to work on all compilers, so use it with care if you want to keep your code portable.

And finally, this page gives some more background information as to why void main is bad.
搜索更多相关主题的帖子: people 
2011-01-10 14:01
li_danwang
Rank: 4
来 自:鄂州
等 级:业余侠客
帖 子:112
专家分:203
注 册:2010-11-12
收藏
得分:0 
void main() //返回类型为空  参数为空
 int main() / /返回类型为int  参数为空
 int main(void) // 返回类型为int  参数为多种类型均可


没事来C一下...   
2011-01-11 14:44
aaronhexin
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:67
专家分:199
注 册:2010-12-6
收藏
得分:0 
A common misconception is that the same logic can be applied to main(). Well, it can't, main() is special, you should always follow the standard and define the return type as int.
通常有种误解,认为这(使用void定义函数,不返回任何值)也能适用于main函数,但是,不行,main()是特殊的,你必须时时刻刻都遵循标准,定义返回类型为int
2011-01-11 20:10
快速回复:What's the difference between main() / void main() / int main() / ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037201 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved