返回信息流☆─────────────────────────────────────☆
ccjguangzhou (happy快乐) 于 (Sat Mar 13 18:22:17 2010) 提到:
请问大牛,在devC++中,为什么不能用void main(),而得用int main()
用void main()会提示you can't use void before main
另外,运行时,dos窗口闪一下就不见了,根本来不及看结果,求大牛指教如何解决
☆─────────────────────────────────────☆
wks (cloverprince) 于 (Sat Mar 13 18:32:29 2010) 提到:
打屁屁。Bjarne Stroustrup叔叔又要生气了。
---------------------------------
Bjarne叔叔的FAQ: http://www2.research.att.com/~bs/bs_faq2.html#void-main
Can I write "void main()"?
The definition
void main() { /* ... */ }
is not and never has been C++, nor has it even been C. See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1. A conforming implementation accepts
int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }
A conforming implementation may provide more versions of main(), but they must all have return type int. The int returned by main() is a way for a program to return a value to "the system" that invokes it. On systems that doesn't provide such a facility the return value is ignored, but that doesn't make "void main()" legal C++ or legal C. Even if your compiler accepts "void main()" avoid it, or risk being considered ignorant by C and C++ programmers.
In C++, main() need not contain an explicit return statement. In that case, the value returned is 0, meaning successful execution. For example:
#include<iostream>
int main()
{
std::cout << "This program returns the integer value 0\n";
}
Note also that neither ISO C++ nor C99 allows you to leave the type out of a declaration. That is, in contrast to C89 and ARM C++ ,"int" is not assumed where a type is missing in a declaration. Consequently:
#include<iostream>
main() { /* ... */ }
is an error because the return type of main() is missing.
----------------------------------------------
中文版: http://www2.research.att.com/~bs/bstechfaq.htm
Q: 我可以写"void main()"吗?
A: 这样的定义
void main() { /* ... */ }
不是C++,也不是C。(参见ISO C++ 标准 3.6.1[2] 或 ISO C 标准 5.1.2.2.1) 一个遵从标准的编译器实作应该接受
int main() { /* ... */ }
和
int main(int argc, char* argv[]) { /* ... */ }
编译器也可以提供main()的更多重载版本,不过它们都必须返回int,这个int是返回给你的程序的调用者的,这是种“负责”的做法,“什么都不返回”可不大好哦。如果你程序的调用者不支持用“返回值”来交流,这个值会被自动忽略——但这也不能使void main()成为合法的C++或C代码。即使你的编译器支持这种定义,最好也不要养成这种习惯——否则你可能被其他C/C++认为浅薄无知哦。
在C++中,如果你嫌麻烦,可以不必显式地写出return语句。编译器会自动返回0。例如:
#include<iostream>
int main()
{
std::cout << "This program returns the integer value 0\n";
}
麻烦吗?不麻烦,int main()比void main()还少了一个字母呢 :O)另外,还要请你注意:无论是ISO C++还是C99都不允许你省略返回类型定义。这也就是说,和C89及ARM C++[译注:指Margaret Ellis和Bjarne Stroustrup于1990年合著的《The Annotated C++ Reference Manual》中描述的C++]不同,int并不是缺省返回值。所以,
#include<iostream>
main() { /* ... */ }
会出错,因为main()函数缺少返回类型。
☆─────────────────────────────────────☆
wks (cloverprince) 于 (Sat Mar 13 18:35:39 2010) 提到:
另外,程序运行完了,就该退出嘛,所以窗口也消失了。
试试退出之前执行system("pause");
☆─────────────────────────────────────☆
FadeToBlack (拜萝卜教 | gfw nmb) 于 (Sat Mar 13 18:35:48 2010) 提到:
你问了两个置顶里存在的问题,请问你是现任bm的拖吗?
【 在 ccjguangzhou (happy快乐) 的大作中提到: 】
: 请问大牛,在devC++中,为什么不能用void main(),而得用int main()
: 用void main()会提示you can't use void before main
: 另外,运行时,dos窗口闪一下就不见了,根本来不及看结果,求大牛指教如何解决
: ...................
☆─────────────────────────────────────☆
wks (cloverprince) 于 (Sat Mar 13 19:27:27 2010) 提到:
不过,建议把Bjarne的FAQ也加入顶置。
☆─────────────────────────────────────☆
ythe (ythe) 于 (Sat Mar 13 20:18:06 2010) 提到:
system("pause");
☆─────────────────────────────────────☆
jkfbrant (迦楼罗) 于 (Sat Mar 13 23:56:33 2010) 提到:
都是被谭浩强害的,他的书里我记得是void main(),居然还那么畅销...
☆─────────────────────────────────────☆
y0192837465 (kimi) 于 (Sun Mar 14 10:17:04 2010) 提到:
system( "pause" )
用这个解决问题
☆─────────────────────────────────────☆
shenlei (我爱果子|[路]|天山南北|潇湘隐士) 于 (Sun Mar 14 11:14:26 2010) 提到:
之后的版本修改了...
【 在 jkfbrant 的大作中提到: 】
: 都是被谭浩强害的,他的书里我记得是void main(),居然还那么畅销...
这是一条镜像帖。来源:北邮人论坛 / cpp / #36623同步于 2010/3/14
CPP机器人发帖
[合集] 关于devC++的问题
jokerlee
2010/3/14镜像同步0 回复
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。