开发者

Returning ints versus void [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

What is the proper declaration of main?

A little while ago I took a course on c++ algorithms. We used visual studio in the classroom and sometimes when writing the main function we would declare it void main() { as opposed to int main(){return 0;} . This worked fine although I knew it normally you should use int. Just recently I tried compiling the same code from the class in netbeans in linux. This time, it would not accept the main function to be declared void. Why is it that the g++ compiler 开发者_StackOverflow社区requires the main function to be declared an int? Sorry if this is a silly question.


The standard says that it needs to be int main() (emphasis here on the return type, not the arguments).

void main() is not standard, and as such bound to break on various platforms.


I remember that every program has a return value, although I don't remember whether or not it just returns a certain number by default. In assembly, you can just say return, if I recall correctly, and not have to give a value.

Or it could just be that it's just not the standard for C++ compilers, like EboMike said.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜