What's difference between main() / void main() / int main() / int main(void) ......?

本文详细解释了C/C++语言中main函数的不同版本及其用法,包括int main()和void main()的区别,并讨论了如何正确使用main函数,确保代码的标准化和可移植性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值